John Sing

Ranch Hand

Posts: 58

posted 1 year ago

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

"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

posted 1 year ago

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

For example, this

would print this:

See if you can use something like that.

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!1

0

Done!

See if you can use something like that.

"Il y a peu de choses qui me soient impossibles..."

posted 1 year ago

I think others are dealing with the mechanics of your problem; however, there is another

Let me give you a clue: The answer involves

HIH

Winston

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Piet Souris

Master Rancher

Posts: 2044

75

posted 1 year ago

Ooops. Removed (hopefully not too late ).

Even if it isn't, a

Winston

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

Articles by Winston can be found here