This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Starting the program over from a certain point?

 
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

 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java 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.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
Bartender
Posts: 3756
154
  • 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: 10777
71
Hibernate Eclipse IDE 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
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!