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

#1 HardQuor
Member since 2007 • 1282 Posts

So I'm pretty new to C++, and I'm in need of a little help trying to understand the way my code is being interpreted. I'm not completely clueless, as I've read a few texts at decent length; but my problem is that i've got very little experience in application. I've successfully programmed simple things like a program to change pennies into dollars, quarters, nickels, and dimes.

Anyhow, I read about this Project Euler thing and I thought it was a perfect chance for me to get some practice. But I've failed on the very first problem many times already, and i've approached the problem in many ways, yet nothing i do seems to work. Anyone else fluent in C++ that can point out where I've screwed up?

The problem asks for the sum of all the multiples of 3 and 5 that are less than 1000. The following is my code.

do{
checking++;
     if( (checking % 3) == 0 || (checking % 5) == 0 );
          {
               sum = sum + checking;
          };
    }while(checking < 999);


---edit---
Glitchspot is not agreeing with me today :(

Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#2 GabuEx
Member since 2006 • 36552 Posts

You have a semicolon after the end bracket of the if statement, which causes the if statement to execute a null command and causes the stuff in the brackets to execute unconditionally.

You're also using a do...while when a for loop would be better, but that doesn't affect functionality. :P

Avatar image for 194197844077667059316682358889
194197844077667059316682358889

49173

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#3 194197844077667059316682358889
Member since 2003 • 49173 Posts
I definitely agree with Gabu; do...while is one of the most annoying constructs in C/C++, and that's really saying something :) Also, you'll either want a condition of less than 1000 or less than/equal to 999 I would have used actual code examples, but apparently the HTML editor doesn't like less than/greater than characters being used.
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#4 HardQuor
Member since 2007 • 1282 Posts

You have a semicolon after the end bracket of the if statement, which causes the if statement to execute a null command and causes the stuff in the brackets to execute unconditionally.

You're also using a do...while when a for loop would be better, but that doesn't affect functionality. :P

GabuEx
OMG. I hate you so much right now, Gabu. This WHOLE time it was just that stupid semicolon. I had no idea that could or would do that. Aside from that, you both agreed that do-while loops were disagreeable.. why? one of my points of confusion is concerning the different loops and why some are more useful in different situations. It seems to me like a while, do-while, and a for loop can each be coded to do the same things. so where's the distinction?
Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#5 GabuEx
Member since 2006 • 36552 Posts
Aside from that, you both agreed that do-while loops were disagreeable.. why? one of my points of confusion is concerning the different loops and why some are more useful in different situations. It seems to me like a while, do-while, and a for loop can each be coded to do the same things. so where's the distinction?HardQuor
What you're doing there is precisely what a for loop is intended for: initializing a value, then looping until it equals a value, incrementing it after each iteration. That logic was so common that they made a special kind of loop just for that situation. While loops are better suited to cases where you aren't iterating from a minimum value to a maximum value, and do...while loops are suited to while loop cases where you always want to execute the contents in the loop at least once no matter what.
Avatar image for 194197844077667059316682358889
194197844077667059316682358889

49173

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#6 194197844077667059316682358889
Member since 2003 • 49173 Posts
[QUOTE="GabuEx"]

You have a semicolon after the end bracket of the if statement, which causes the if statement to execute a null command and causes the stuff in the brackets to execute unconditionally.

You're also using a do...while when a for loop would be better, but that doesn't affect functionality. :P

HardQuor
OMG. I hate you so much right now, Gabu. This WHOLE time it was just that stupid semicolon. I had no idea that could or would do that. Aside from that, you both agreed that do-while loops were disagreeable.. why? one of my points of confusion is concerning the different loops and why some are more useful in different situations. It seems to me like a while, do-while, and a for loop can each be coded to do the same things. so where's the distinction?

Yeah, as Gabu mentioned, they are semantically interchangeable; the only thing I'd add to his excellent summary is that, at least in my mind, having the condition at the beginning (while loop) is much more readable than having the condition at the end (do...while loop). Admittedly, that could just be the result of having been exposed to that as a coding standard in my work environments the last 16 years, though :)
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#7 HardQuor
Member since 2007 • 1282 Posts
well, that all makes sense, i suppose. I think it's just my inexperience preventing me from seeing the obvious. also, before this thread dies, are either of you familiar with Project Euler? I'm just curious as to how far you guys have gotten or can get, whichever the case may be.
Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#8 OrkHammer007
Member since 2006 • 4753 Posts

Try this instead:

for(checking = 1; checking < 1000; ++checking)
{
   if(checking%3==0||checking%5==0)sum=sum+checking;
}

I guess C++/Java doesn't agree with GS.

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#9 HardQuor
Member since 2007 • 1282 Posts

Try this instead:

for(checking = 1; checking < 1000; ++checking)
{
   if(checking%3==0||checking%5==0)sum=sum+checking;
}

I guess C++/Java doesn't agree with GS.

OrkHammer007
Yeah, i know how to code a for loop, it's just that the do-while loop that i posted was the first thing that came to mind. Thanks for the help, though :) And i might just be resurrecting this thread in the very near future again. Problem 2 is looking a lot more complicated O_o;
Avatar image for Stesilaus
Stesilaus

4999

Forum Posts

0

Wiki Points

0

Followers

Reviews: 1

User Lists: 0

#10 Stesilaus
Member since 2007 • 4999 Posts

I definitely agree with Gabu; do...while is one of the most annoying constructs in C/C++, and that's really saying something :) Also, you'll either want a condition of less than 1000 or less than/equal to 999 I would have used actual code examples, but apparently the HTML editor doesn't like less than/greater than characters being used.xaos

Heresy!

I always try to cast all my loops as "do ... while" loops!

Yes, really, and I have a good reason too: A "do ... while" loop has just one conditional jump, whereas a "for loop" or "while loop" compiles down to code with a conditional jump and an unconditional jump. The "do ... while" loop is a more efficient construct.

Yeah, I know you're gonna say "The optimizer takes care of that!". But I don't have enough faith in compiler optimizers to abandon my "do ... while" fixation. :P

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#11 HardQuor
Member since 2007 • 1282 Posts

Heh, i know it's silly, but a do..while loop reads more humanely than for loops do to me. Again, i'm sure it's just my inexperience speaking.

anywho, i've got another problem with the second project euler problem. (oy. at this rate, i'll NEVER get all 217 :()
The problem asks for the sum of all the even numbers that are less than 4 million that appear in the Fibonacci sequence. The following is my code.

do
{
term3 = term1 + term2;
if( term1 % 2 == 0 )
{
sum = term1 + sum;
}
else if( term2 % 2 == 0 )
{
sum = term2 + sum;
};
term1 = term2;
term2 = term3;
}while( term3 < 4000000 )

I have a feeling that my integers need to be doubles but when i do that, i get a compile-time error telling me that i cant use a binary operator (%) on double integers...

Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#12 OrkHammer007
Member since 2006 • 4753 Posts

Don't use a double; it'll end up being a decimal instead of the integer you want. Use a long int instead (ex. long int term3 = 0;).

Otherwise, it looks pretty good. :D

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#13 HardQuor
Member since 2007 • 1282 Posts

Don't use a double; it'll end up being a decimal instead of the integer you want. Use a long int instead (ex. long int term3 = 0;).

Otherwise, it looks pretty good. :D

OrkHammer007
no good, it's still giving me the same answer. I tried unsigned as well, that didn't work either :(

I just don't understand what is going on here. At least i know it's not just me this time..
Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#14 OrkHammer007
Member since 2006 • 4753 Posts

I just copy/pasted what you wrote, with the following initial values:

long int term1 = 0; long int term2 = 1; long int term 3 = 0; long int sum = 0;

It compiled and ran fine. What's the answer supposed to be (I get  5702886)?

Oh... and I added a ; after the "while" part of the loop.

Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#15 OrkHammer007
Member since 2006 • 4753 Posts

Bloody hell... the problem just smacked me right in the face:

Why are you checking term1 and term2, when the Fibonacci term is term3? That's the only one you need to check. :P

Sorry I didn't catch that earlier.

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#16 HardQuor
Member since 2007 • 1282 Posts

I just copy/pasted what you wrote, with the following initial values:

long int term1 = 0; long int term2 = 1; long int term 3 = 0; long int sum = 0;

It compiled and ran fine. What's the answer supposed to be (I get  5702886)?

Oh... and I added a ; after the "while" part of the loop.

OrkHammer007

I just copy/pasted what you wrote, with the following initial values:

long int term1 = 0; long int term2 = 1; long int term 3 = 0; long int sum = 0;

It compiled and ran fine. What's the answer supposed to be (I get  5702886)?

Oh... and I added a ; after the "while" part of the loop.

OrkHammer007
I wish i knew the answer i'm supposed to be getting, but alas, the website won't tell me until i've got it right. Interestingly enough, the way the problem was phrased, i assumed my first two term values should be 1 and 2, but when i plugged in 0 and 1 (which make more sense) it gave me a different answer. Except the answer isn't the one you posted.. odd. instead, i get 59113738.
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#17 HardQuor
Member since 2007 • 1282 Posts

Bloody hell... the problem just smacked me right in the face:

Why are you checking term1 and term2, when the Fibonacci term is term3? That's the only one you need to check. :P

Sorry I didn't catch that earlier.

OrkHammer007
Ugh. I think you nailed it. I can't believe the mistakes i'm making :( I think it's going to be a long, frustrating road ahead :(
Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#18 OrkHammer007
Member since 2006 • 4753 Posts

Try this instead:

do{

term3 = term1 + term2;

if(term3%2==0)sum+=term3;

term1=term2;

term2=term3;

}while(term3 "less than symbol"4000000);

Stupid HTML conflict!!!

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#19 HardQuor
Member since 2007 • 1282 Posts

Well. I shortened the code to evaluate only term3:

do
{
    term3 = term1 + term2;
    if( term3 % 2 == 0 )
    {
        sum = term3 + sum;
     };
     term1 = term2;
     term2 = term3;
}while( term3 (less than or equal to) 4000000 );

and i get 58024584. But that's still wrong. I still don't know what the hell i'm doing wrong. this is pissing me off.
oh, also, term1 and term2 have been reduced to 0 and 1, respectively.

Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#20 GabuEx
Member since 2006 • 36552 Posts

[QUOTE="xaos"]I definitely agree with Gabu; do...while is one of the most annoying constructs in C/C++, and that's really saying something :) Also, you'll either want a condition of less than 1000 or less than/equal to 999 I would have used actual code examples, but apparently the HTML editor doesn't like less than/greater than characters being used.Stesilaus

Heresy!

I always try to cast all my loops as "do ... while" loops!

Yes, really, and I have a good reason too: A "do ... while" loop has just one conditional jump, whereas a "for loop" or "while loop" compiles down to code with a conditional jump and an unconditional jump. The "do ... while" loop is a more efficient construct.

Yeah, I know you're gonna say "The optimizer takes care of that!". But I don't have enough faith in compiler optimizers to abandon my "do ... while" fixation. :P

If you're coding based on how many jumps there are in the resulting assembly, and you're not coding for a microprocessor, you're doing it wrong. :P
Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#21 OrkHammer007
Member since 2006 • 4753 Posts
[QUOTE="HardQuor"] I can't believe the mistakes i'm making :( I think it's going to be a long, frustrating road ahead :(

Don't worry so much about it. Everyone makes mistakes in the beginning. :D Getting past those mistakes, setting aside the frustration... that's when the fun really begins...
Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#22 GabuEx
Member since 2006 • 36552 Posts

Getting past those mistakes, setting aside the frustration... that's when the fun really begins...OrkHammer007

Yeah, once you get past those mistakes, you can get on to code more complicated things and have really aggravatingly hard-to-spot mistakes. :P

Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#23 OrkHammer007
Member since 2006 • 4753 Posts

[QUOTE="OrkHammer007"]Getting past those mistakes, setting aside the frustration... that's when the fun really begins...GabuEx

Yeah, once you get past those mistakes, you can get on to code more complicated things and have really aggravatingly hard-to-spot mistakes. :P

...and that's not fun? :|

:lol:

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#24 HardQuor
Member since 2007 • 1282 Posts
So, no one has any clue, 'eh?
Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#25 OrkHammer007
Member since 2006 • 4753 Posts

So, no one has any clue, 'eh?HardQuor
That last try didn't get it? :? I could swear it was right.

I just rewrote it to spit out the Fibonacci numbers so I could double check (using Wikipedia as a reference to make sure I got the right calculations), reran the sum, added the numbers on a calculator to triple check my results, and got...

[spoiler] 4613732 [/spoiler] ...which came straight from the last code snippet you posted.

Avatar image for CoreoVII
CoreoVII

1838

Forum Posts

0

Wiki Points

0

Followers

Reviews: 7

User Lists: 0

#26 CoreoVII
Member since 2007 • 1838 Posts
Interesting, I just took a Introduction into programming using C++. Pretty boring, don;t think it matches the type of person I am. God luck man.
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#27 HardQuor
Member since 2007 • 1282 Posts

[QUOTE="HardQuor"]So, no one has any clue, 'eh?OrkHammer007

That last try didn't get it? :? I could swear it was right.

I just rewrote it to spit out the Fibonacci numbers so I could double check (using Wikipedia as a reference to make sure I got the right calculations), reran the sum, added the numbers on a calculator to triple check my results, and got...

4613732 ...which came straight from the last code snippet you posted.

I've never gotten my code to get that output.. I'm guessing that's probably right, but i don't want to put it in until i can get my code to back it up.

---edit---
aha! i found out what the problem is, by doing what you did, i just had it output term3 at the end of each do..while loop. aaand, it turns out that it's going one step over the 4 million mark for some reason.. gotta figure that out, now.

---re-edit---
nope, I was wrong again, i was actually reading a list of all thefibonacci sequence numbers, not the ones that were being totalled. So now i'm just totally confused, because when i checked the numbers being added together, it was all the rightones, and like you, i added them together manually to get the same sum that you got. But for some reason, my code is coming up with an extra 53410852 somewhere.

---re-re-edit---
OK! I FINALLY figured it out. Turns out that if i don't initialize a long integer, it defines itself as 53410852. So i went ahead and initialized it with a value of 0 and got our 4 million-ish number :lol: I knew from reading some books on C++ that it would be finicky, but JEEZ.

Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#28 OrkHammer007
Member since 2006 • 4753 Posts
I've never gotten my code to get that output.. I'm guessing that's probably right, but i don't want to put it in until i can get my code to back it up.

---edit---
aha! i found out what the problem is, by doing what you did, i just had it output term3 at the end of each do..while loop. aaand, it turns out that it's going one step over the 4 million mark for some reason.. gotta figure that out, now.

---re-edit---
nope, I was wrong again, i was actually reading a list of all thefibonacci sequence numbers, not the ones that were being totalled. So now i'm just totally confused, because when i checked the numbers being added together, it was all the rightones, and like you, i added them together manually to get the same sum that you got. But for some reason, my code is coming up with an extra 53410852 somewhere.

---re-re-edit---
OK! I FINALLY figured it out. Turns out that if i don't initialize a long integer, it defines itself as 53410852. So i went ahead and initialized it with a value of 0 and got our 4 million-ish number :lol: I knew from reading some books on C++ that it would be finicky, but JEEZ.

HardQuor

Edit #1- It will come up with the extra 5+ million because of the loop. The value before that one is just shy of the 4 million condition you set; therefore, you will get the extra iteration. Fortunately, it's an odd value, which fails the if... test in the loop.

Edit #3: One of the first rules we learned in programming (whether it was C++ or Java or Visual Basic) was to always initialize a variable. There are some instances where a "null" value is needed, but even then, we were told to intialize it as a "null."

Did the initialization solve your problem?

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#29 HardQuor
Member since 2007 • 1282 Posts
[QUOTE="HardQuor"]I've never gotten my code to get that output.. I'm guessing that's probably right, but i don't want to put it in until i can get my code to back it up.

---edit---
aha! i found out what the problem is, by doing what you did, i just had it output term3 at the end of each do..while loop. aaand, it turns out that it's going one step over the 4 million mark for some reason.. gotta figure that out, now.

---re-edit---
nope, I was wrong again, i was actually reading a list of all thefibonacci sequence numbers, not the ones that were being totalled. So now i'm just totally confused, because when i checked the numbers being added together, it was all the rightones, and like you, i added them together manually to get the same sum that you got. But for some reason, my code is coming up with an extra 53410852 somewhere.

---re-re-edit---
OK! I FINALLY figured it out. Turns out that if i don't initialize a long integer, it defines itself as 53410852. So i went ahead and initialized it with a value of 0 and got our 4 million-ish number :lol: I knew from reading some books on C++ that it would be finicky, but JEEZ.

OrkHammer007

Edit #1- It will come up with the extra 5+ million because of the loop. The value before that one is just shy of the 4 million condition you set; therefore, you will get the extra iteration. Fortunately, it's an odd value, which fails the if... test in the loop.

Edit #3: One of the first rules we learned in programming (whether it was C++ or Java or Visual Basic) was to always initialize a variable. There are some instances where a "null" value is needed, but even then, we were told to intialize it as a "null."

Did the initialization solve your problem?

yup! that 4million number was the correct answer, thankfully. Now i'm moving on up to problem 6. I haven't done 3-5 yet, as i'm doing them in the order of the number of people who have completed them succesfully. Apparently problem 6 is easier than 3-5.
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#30 HardQuor
Member since 2007 • 1282 Posts
weird, glitchspot doesn't want to let me edit my posts. Anyways, I probably should have known to always initialize my variables, but it was so long ago that i last read a C++ book, that i'm going on memory alone. At least now i'm learning through application instead of just reading. Can't afford a programming class at this point :(
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#31 HardQuor
Member since 2007 • 1282 Posts

It's zombie thread time. sorry.
Anyhow, this time i'm more confuzzled than ever before. I got through problem 6 just fine, but the next step up the difficulty ladder (problem 5), is proving much more complicated. Here's the problem.

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

and my code so far

int x=0;
for( int n=0; n (less than or equal to) 10; n++ )
{
     if( (x % n) != 0 )
     {
         x++;
     };
};

I'm only doing the first 10 as a proof of concept, since they give me the answer for that, so i can tell when i've done something wrong. As far as i can tell, i think i'm on the right path. "For each number up to ten, if x is not divisible by it, then increase x by 1.." it compiles fine, but it crashes at runtime for some reason. Anyone know what's going on? :?

Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#32 GabuEx
Member since 2006 • 36552 Posts

It's zombie thread time. sorry.
Anyhow, this time i'm more confuzzled than ever before. I got through problem 6 just fine, but the next step up the difficulty ladder (problem 5), is proving much more complicated. Here's the problem.

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

and my code so far

int x=0;
for( int n=0; n (less than or equal to) 10; n++ )
{
     if( (x % n) != 0 )
     {
         x++;
     };
};

I'm only doing the first 10 as a proof of concept, since they give me the answer for that, so i can tell when i've done something wrong. As far as i can tell, i think i'm on the right path. "For each number up to ten, if x is not divisible by it, then increase x by 1.." it compiles fine, but it crashes at runtime for some reason. Anyone know what's going on? :?

HardQuor

Ask yourself this question: what math is the program attempting when n = 0?

(There are other problems with the program, but they aren't what's making it crash.)

Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#33 OrkHammer007
Member since 2006 • 4753 Posts

You're going to need 2 loops for this one: one to increment your target number, and one to loop over your divisions.

Try:

bool check=false;

int x=1;

int target=1;

while(!check){ *Note: a do...while will work, too, if it makes you more comfortable.

*second loop goes here, much like the one you put up earlier*

I used a "break" condition if (target%x!=0) here, and an else-if to check if x==20 and change the boolean to "true."

If the loop exited and check was still false, I incremented the target.

}

Then I spat it to the command line, and done.

It took forever to run: the answer is HUGE.

Hope this helps. :D

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#34 HardQuor
Member since 2007 • 1282 Posts
Ask yourself this question: what math is the program attempting when n = 0?

 

(There are other problems with the program, but they aren't what's making it crash.)

GabuEx
If n = 0, and x = 0 on the first loop, since (0 % 0) == 0, then it fails to enter the if statement, so shouldn't it just increment n and start over?

You're going to need 2 loops for this one: one to increment your target number, and one to loop over your divisions.

Try:

bool check=false;

int x=1;

int target=1;

while(!check){ *Note: a do...while will work, too, if it makes you more comfortable.

*second loop goes here, much like the one you put up earlier*

I used a "break" condition if (target%x!=0) here, and an else-if to check if x==20 and change the boolean to "true."

If the loop exited and check was still false, I incremented the target.

}

Then I spat it to the command line, and done.

It took forever to run: the answer is HUGE.

Hope this helps. :D

OrkHammer007
Hmm. A general rule they have at Project Euler is that a program run on a decent computer should have no problem rendering the solution to any of it's problems in under a minute. Also, i'm having a hard time understanding what you're trying to describe. You said you included a loop like mine in your code, but i couldn't get my loop to work :?

anyhow, i'm currently trying something new, i'm starting the for loop at 10 and having it decrement after each successful dividend is found, and have gotten that to work wonderfully. Unfortunately, that just makes x a dividend of the next number while being greater than it's own last value (i.e. 10 for 10, 18 for 9, 24 for 8, 28 for 7, etc.)I'm not sure how to make it check it's value against each previous divisor.
Avatar image for dann14v
dann14v

689

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#35 dann14v
Member since 2005 • 689 Posts

Just a quick rule of thumb when it comes to loops:

do-whiles should only be used when you want your program to execute at least one time.

while loops should be used if there is the possibility of your program never executing a certain method. (if the first boolean argument fails, the method will not execute).

for loops should only be used if you know the exact number of times you want to iterate through an algorithm.

I'm sure most of you know this, but just in case =]

Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#36 OrkHammer007
Member since 2006 • 4753 Posts

Hopefully, you can read the code from the screen shot; since the forum code doesn't like C++/Java, it'll probably work a bit better like this.

It's written in Java (I'm a bit more comfortable with it), but it should be simple to convert it to C++.

It took about 25 seconds to run, and it came up with a figure in 9 digits... :shock:

Hopefully, this helps. :D

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#40 HardQuor
Member since 2007 • 1282 Posts

Hopefully, you can read the code from the screen shot; since the forum code doesn't like C++/Java, it'll probably work a bit better like this.

It's written in Java (I'm a bit more comfortable with it), but it should be simple to convert it to C++.

It took about 25 seconds to run, and it came up with a figure in 9 digits... :shock:

Hopefully, this helps. :D

OrkHammer007

oops, i didn't get to see your post before i decided to see my methodology to the end :?

but i do have good news, i got it!

    int x=20;
    int y=0;
    for( int n=20; n > 1; n-- )
    {
         while( (x % n) != 0 )
         {
                x = x + y;
         };
         y = x;
    };

I figured that my mistake would be easily corrected by just incrementing x by it's last known value instead of by 1. And even better, the code is simpler and it rendered instantly :) thanks for your help and sorry i wasn't able to consider your contribution :?

By the by, I hope i'm not annoying you guys with my constant questions? :?

Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#41 GabuEx
Member since 2006 • 36552 Posts

since (0 % 0) == 0HardQuor

Nope. x % 0 for any x is asking "what is the remainder when x is divided by 0"? Division by 0 is obviously not allowed, which thus causes your program to crash.

oops, i didn't get to see your post before i decided to see my methodology to the end :?

but i do have good news, i got it!

 

    int x=20;
    int y=0;
    for( int n=20; n > 1; n-- )
    {
         while( (x % n) != 0 )
         {
                x = x + y;
         };
         y = x;
    };

I figured that my mistake would be easily corrected by just incrementing x by it's last known value instead of by 1. And even better, the code is simpler and it rendered instantly :) thanks for your help and sorry i wasn't able to consider your contribution :?

By the by, I hope i'm not annoying you guys with my constant questions? :?

HardQuor

Actually, there's an even easier way to do this that doesn't need two loops or extra variables:

    int x = 1;

    for (int n = 2; n (less than or equal to) 20; n++)
    {
         if (x % n != 0)
         {
                x++;
                n = 1;
         }
    }

This basically loops through all values of n, and if it finds that x is not divisible by one of the values, then it increments x by 1 and starts over.

Of course, this is very brute force, and there is a very elegant way of figuring it out if you add in a bit of smart math, but this nonetheless works.

Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#42 OrkHammer007
Member since 2006 • 4753 Posts
I figured that my mistake would be easily corrected by just incrementing x by it's last known value instead of by 1. And even better, the code is simpler and it rendered instantly :) thanks for your help and sorry i wasn't able to consider your contribution :?

By the by, I hope i'm not annoying you guys with my constant questions? :?

HardQuor

...I'm still trying to figure out why that worked... I'm a bit dense at the moment: programming challenges tend to cost me sleep (one time, while I was taking C++ 1 in college, I was at the PC for 12 straight hours because I literally couldn't sleep until I figured out the method that output a pyramid on the screen).

Don't worry about annoying me: I tutored programming (VB, C++, and Java) to pay part of my tuition, and I love a good coding challenge (even if it aggravates my insomnia :lol: ). Ask away!

Oh... and I think GS is running part of your code: somehow, you quadruple-posted. :shock:

Actually, there's an even easier way to do this that doesn't need two loops or extra variables:GabuEx
Yeah... but it's not as evil and cryptic-looking like mine is... :lol:

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#43 HardQuor
Member since 2007 • 1282 Posts

[QUOTE="HardQuor"]since (0 % 0) == 0GabuEx

Nope. x % 0 for any x is asking "what is the remainder when x is divided by 0"? Division by 0 is obviously not allowed, which thus causes your program to crash.

Ahh. Now i get it. I feel dense :(

Actually, there's an even easier way to do this that doesn't need two loops or extra variables:

    int x = 1;

    for (int n = 2; n (less than or equal to) 20; n++)
    {
         if (x % n != 0)
         {
                x++;
                n = 1;
         }
    }

GabuEx
I should've known better than to think that my code was optimal, you just blew my mind!:shock::lol:

This basically loops through all values of n, and if it finds that x is not divisible by one of the values, then it increments x by 1 and starts over.

Of course, this is very brute force, and there is a very elegant way of figuring it out if you add in a bit of smart math, but this nonetheless works.

GabuEx
was that? could it be? did you complement my code, or am i delusional? :lol:
Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#44 HardQuor
Member since 2007 • 1282 Posts
...I'm still trying to figure out why that worked... OrkHammer007
I'm glad you left me an opening to explain my methodology, i'm a little proud of it (more than i should be, anyway :P). The reason is because i approached it more like a human approaches similar problems. When we try to find lowest common denominators, we start from the highest number and work our way down. Just think about breaking change, you usually start with quarters first, then dimes, nickels, and finally pennies. So instead of incrementing n, i decided to decrement it.

Then i had the problem of making x divisible by each previous term as well as the one it was in the loop for. I figured my problem was that i was incrementing x by 1, which is obviously not divisible by any number but 1. so since i was decrementing n anyways, it made sense to just increment x by it's previous value, since those are the smallest increments that are divisible by each previous value of n.
I'm a bit dense at the moment: programming challenges tend to cost me sleep (one time, while I was taking C++ 1 in college, I was at the PC for 12 straight hours because I literally couldn't sleep until I figured out the method that output a pyramid on the screen).

 

OrkHammer007
haha, I've experienced similar frustrations with sleep since i've started Project Euler :P

Don't worry about annoying me: I tutored programming (VB, C++, and Java) to pay part of my tuition, and I love a good coding challenge (even if it aggravates my insomnia :lol: ). Ask away!

OrkHammer007
that's good news! :) originally, i had thought to get more responses from others around the boards, but since it's just basically three of us, i wanted to make sure it was cool with you and gabu if i kept pestering you :P

also, seeing as you're such a big fan of these challenges, are you interested in checking out Project Euler yourself?

Oh... and I think GS is running part of your code: somehow, you quadruple-posted. :shock:

OrkHammer007
haha, yeah, i thought i was at the "edit post" screen, not the "new post" screen :P
Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#45 GabuEx
Member since 2006 • 36552 Posts

was that? could it be? did you complement my code, or am i delusional? :lol:HardQuor

Well, yours was a bit better, but there's actually a very much more streamlined way of doing this.  If you're curious about what that is, it comes from considering the prime numbers that make up each number from 2 to 20 (we'll ignore 1 since everything is divisible by 1).  For example:

2 = 2
3 = 3
4 = 2 x 2
5 = 5
6 = 2 x 3

...and so on.

The smallest number divisible by all the numbers from 2 to 20 is the number that includes as its factors the prime numbers that make up each number, and no more.  So, for example, the smallest number divisible by the numbers 2 to 6 would be:

2 x 3 x 2 x 5 = 60

To make it divisible by 2, you add 2; for 3, you add 3; for 4, you add 2 (since one 2 was already present); for 5, you add 5; and for 6, you add nothing (since both 2 and 3 were already present).

So, basically, what you need to do is just start a list of prime factors as an empty set, and then find the prime factors of all of the numbers from 2 to 20 and then test to see whether or not they're already in the list.  Once you've done this for all numbers, you then just multiply all of the prime numbers together and you have your answer.

This requires code that's rather more complicated than what we've got here, though.  Just for fun, though, I implemented it as a proof of concept; it pretty much instantly finds the answer, even if you make it much larger and have it find the smallest number divisible by 1 to 500.

Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#46 OrkHammer007
Member since 2006 • 4753 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. ;)

Avatar image for HardQuor
HardQuor

1282

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#47 HardQuor
Member since 2007 • 1282 Posts

Well, yours was a bit better, but there's actually a very much more streamlined way of doing this.  If you're curious about what that is,

...

This requires code that's rather more complicated than what we've got here, though.  Just for fun, though, I implemented it as a proof of concept; it pretty much instantly finds the answer.

 

GabuEx
Yeah, that's way over my head :( I'll just consider my solution a good compromise between conciseness and elegance :P
Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#48 GabuEx
Member since 2006 • 36552 Posts

Yeah, that's way over my head :( I'll just consider my solution a good compromise between conciseness and elegance :PHardQuor

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).

Avatar image for KOTORkicker
KOTORkicker

4595

Forum Posts

0

Wiki Points

0

Followers

Reviews: 8

User Lists: 0

#49 KOTORkicker
Member since 2007 • 4595 Posts

Try this instead:

for(checking = 1; checking < 1000; ++checking)
{
   if(checking%3==0||checking%5==0)sum=sum+checking;
}

I guess C++/Java doesn't agree with GS.

OrkHammer007
No type of formatting text agrees with GS, it would seem.
Avatar image for OrkHammer007
OrkHammer007

4753

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#50 OrkHammer007
Member since 2006 • 4753 Posts
No type of formatting text agrees with GS, it would seem.KOTORkicker
It could be worse: since Java and C++ look a lot like Javascript, it could reject the whole post... or *shudder* interpret the code and run it.