• Post Reply Bookmark Topic Watch Topic
  • New Topic

Starting the program over from a certain point?  RSS feed

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am having trouble writing a program and I was hoping I could get some help. I have to write a program to solve the following 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 positive number that is evenly divisible by all of the numbers from 1 to 20?"

so I know how to check if it divides equally. So I have a bunch of "if" loops and if it does not divide equally then it is supposed to add one to the number variable and start the program over. The problem is my current code adds one but does not start the program over. How can I get this to work? Here is my current code

 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, what you need is a loop. Java supports a few different kinds, but I think you could use a "while" loop. It's pretty simple, and looks like this:

As long as <boolean expression> is true the code between the curly brackets runs, over and over. The first time that <boolean expression> is false, the next line of code below the closing curly bracket (on Line 4, in my example above) will run, and on downward from there.

For example, this

would print this:

2
1
0
Done!


See if you can use something like that.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:"What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?"
...so I know how to check if it divides equally. So I have a bunch of "if" loops and if it does not divide equally then it is supposed to add one to the number variable and start the program over. The problem is my current code adds one but does not start the program over. How can I get this to work? Here is my current code...

I think others are dealing with the mechanics of your problem; however, there is another way to solve the problem that doesn't involve checking every single number (ie, by "brute force").

Let me give you a clue: The answer involves prime factorization of the numbers you want to divide by.

HIH

Winston
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
be careful when giving solutions! This is problem 5 of Project Euler.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:be careful when giving solutions! This is problem 5 of Project Euler.

Ooops. Removed (hopefully not too late ).

Even if it isn't, a generic solution isn't absolutely straightforward; but thanks for the heads-up.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!