C++ noob needs help :(

  • 63 results
  • 1
  • 2

This topic is locked from further discussion.

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#51 HardQuor
Member since 2007 • 1282 Posts

I thought about trying the Project Euler thing when you brought it up.

On the one hand, I like the programming part. If I didn't, then I put a lot of effort into getting a degree in something I hate (and got near-straight A's in it, too... bloody interface design :evil: )... :lol:

On the other hand, I don't do well with numbers (something that might baffle other programmers, and my math teachers in college). It took me several tries to finally understand algebra (high school, two math courses in the Navy, then it finally clicked in college... where I tutored math, too... :? ), and arithmetic can get me in trouble (9 * 7 != 16... I know it, I feel it, but it's even money if I actually write it correctly).

I'll ponder some more... I'm leaning towards "go for it," though. ;)

OrkHammer007
I think you should totally go for it, my math skills are quite lacking as well. Hell, i was assigned high school algebra when i took my college placement tests. Besides, the more the merrier :D

[QUOTE="HardQuor"]Yeah, that's way over my head :( I'll just consider my solution a good compromise between conciseness and elegance :PGabuEx

Well, it's actually not too conceptually difficult.  I probably just explained it badly.  There are two main concepts that need to be understood with what I was talking about:

1. First, there are prime numbers (numbers which are divisible by only 1 and itself), and there are composite numbers (numbers which are divisible by more than that).  Any composite number can be represented as a product of prime numbers (for example, 12 = 2 * 2 * 3).

2. Second, to say that x is divisible by y is to say that all prime factors of y are also prime factors of x.  For example, 12 is divisible by 4 because 12 = 2 * 2 * 3, while 4 = 2 * 2 - all prime factors of 4 are also prime factors of 12.

See if what I said makes any more sense in light of those two things (or tell me if the above also makes no sense).

No, i think you explained prime factors just fine (which i'm thankful for, as the next problem for Project Euler deals directly with figuring them out for a ridiculously large number). What is over my head is conceptualizing it all in code. Although, i'm not really dedicating a lot of thought to it, admittedly :P
Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#52 GabuEx
Member since 2006 • 36552 Posts
What is over my head is conceptualizing it all in code. Although, i'm not really dedicating a lot of thought to it, admittedly :PHardQuor
Ohh, so you mean you understood what I was talking about, just not how you'd implement it in code. Well, yes, I did it and I can tell you that it requires a fair bit more complex constructs than just ints and loops. :P
Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#53 OrkHammer007
Member since 2006 • 4753 Posts
...and I can tell you that it requires a fair bit more complex constructs than just ints and loops. :PGabuEx
OK... I'm officially intrigued. The only thing I can think of is an array of primes, and looping through them to modulus the target number, and I have a feeling that's not it. :|
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#54 HardQuor
Member since 2007 • 1282 Posts
Well, problem 3 seems to be a lot simpler than i thought it would be. It asks for the largest prime number of 600 billion something. I've got the code to work on a smaller number, but unfortunately, i've run into the problem of my int data type not being able to contain the full 12 digits of the term. I've tried long ints, unsigned ints, and unsigned long ints. Anyone have a solution?
Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#55 GabuEx
Member since 2006 • 36552 Posts
[QUOTE="GabuEx"]...and I can tell you that it requires a fair bit more complex constructs than just ints and loops. :POrkHammer007
OK... I'm officially intrigued. The only thing I can think of is an array of primes, and looping through them to modulus the target number, and I have a feeling that's not it. :|

I didn't use arrays; I used linked lists, since the latter make it so that you aren't constrained to a set number of elements. Basically, what I did was I created a function that returns a linked list of all the prime factors of a number, and then I generate that list for all the numbers from 2 to 20. I then created the overall linked list from that by going through each list and adding elements from each list if they don't already appear in the overall linked list. I then just multiply all of the elements of the overall linked list together to get the answer.
Well, problem 3 seems to be a lot simpler than i thought it would be. It asks for the largest prime number of 600 billion something. I've got the code to work on a smaller number, but unfortunately, i've run into the problem of my int data type not being able to contain the full 12 digits of the term. I've tried long ints, unsigned ints, and unsigned long ints. Anyone have a solution?HardQuor
Use a double.
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#56 HardQuor
Member since 2007 • 1282 Posts
[QUOTE="GabuEx"] Use a double.

I'd love to, but it won't let me use % with doubles :?
Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#57 GabuEx
Member since 2006 • 36552 Posts

[QUOTE="GabuEx"] Use a double.HardQuor
I'd love to, but it won't let me use % with doubles :?

Include math.h and do (n / m == floor(n / m)) instead.  That's equivalent to (n % m == 0).

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#58 HardQuor
Member since 2007 • 1282 Posts

[QUOTE="HardQuor"][QUOTE="GabuEx"] Use a double.GabuEx

I'd love to, but it won't let me use % with doubles :?

Include math.h and do (n / m == floor(n / m)) instead.  That's equivalent to (n % m == 0).

that was good advice, too, but it says that the "integer constant is too large for "long" type"

Rawr.
Avatar image for Notsogr8one
Notsogr8one

3739

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#59 Notsogr8one
Member since 2004 • 3739 Posts
[QUOTE="GabuEx"]

[QUOTE="HardQuor"] I'd love to, but it won't let me use % with doubles :?HardQuor

Include math.h and do (n / m == floor(n / m)) instead.  That's equivalent to (n % m == 0).

that was good advice, too, but it says that the "integer constant is too large for "long" type"

Rawr.

long long int ftw!
Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#60 GabuEx
Member since 2006 • 36552 Posts
[QUOTE="GabuEx"]

[QUOTE="HardQuor"] I'd love to, but it won't let me use % with doubles :?HardQuor

Include math.h and do (n / m == floor(n / m)) instead.  That's equivalent to (n % m == 0).

that was good advice, too, but it says that the "integer constant is too large for "long" type"

Rawr.

You need to put a decimal point and a 0 at the end of a constant to make it a double; otherwise it'll try to interpret it as an integer, and that number's too big for an integer.
Avatar image for freshgman
freshgman

12241

Forum Posts

0

Wiki Points

0

Followers

Reviews: 1

User Lists: 0

#61 freshgman
Member since 2005 • 12241 Posts
i got a c+ on a test once
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#62 HardQuor
Member since 2007 • 1282 Posts
[QUOTE="HardQuor"][QUOTE="GabuEx"]

 

Include math.h and do (n / m == floor(n / m)) instead.  That's equivalent to (n % m == 0).

GabuEx
that was good advice, too, but it says that the "integer constant is too large for "long" type"

Rawr.

You need to put a decimal point and a 0 at the end of a constant to make it a double; otherwise it'll try to interpret it as an integer, and that number's too big for an integer.

Is it normal to feel stupid even though, in asking for it, i've already accepted that i don't know the answers to my problems? :?

(what i mean to say is thanks for your help :P )
Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#63 OrkHammer007
Member since 2006 • 4753 Posts

[QUOTE="OrkHammer007"]OK... I'm officially intrigued. The only thing I can think of is an array of primes, and looping through them to modulus the target number, and I have a feeling that's not it. :|GabuEx
I didn't use arrays; I used linked lists, since the latter make it so that you aren't constrained to a set number of elements. Basically, what I did was I created a function that returns a linked list of all the prime factors of a number, and then I generate that list for all the numbers from 2 to 20. I then created the overall linked list from that by going through each list and adding elements from each list if they don't already appear in the overall linked list. I then just multiply all of the elements of the overall linked list together to get the answer.

Ah... I see it now... you would end up with a list beginning with 2,3,2,2,5... and ending with ...19,5,2,2. Then a simple loop (checking for the end of the list) to multiply it all together...

Almost seems easier to take a piece of paper, work out the prime factors from 2-20, then grabbing a calculator and physically multiplying it together. ;)

Is it normal to feel stupid even though, in asking for it, i've already accepted that i don't know the answers to my problems? :? HardQuor

Don't feel "stupid"... just feel "inexperienced." I'm pretty sure it's normal for anyone just starting out in programming to feel a bit overwhelmed... especially in C++.

Just keep in mind that, the more you know, the more you can still learn. :D

(Like now... I realize that the cIass I took in Web Scripting is helping me deal with GS's posting quirks... :lol: )