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? My solution int x; int y; for (x = 2520; x >= 2520; x +=10 ) { for (y = 1; y <= 20; y++) { if (x % y == 0) { System.out.println ("The numer is"+x); } } } } }
232792560 is divisable by numbers 1-20, now verify the result given by your program
I know the answer, but how do you get it by the program, my program gives every multiple, and not just that one.
hold on
public static long problem5() { long i = 2520; boolean found = false; while (!found) { i += 2520; boolean divis = true; for (int j = 11; j <= 20; j++) { if (i % j != 0) { divis = false; //System.out.println(i + " is not divisible by " + j); break; } else { //System.out.println(i + " is divisible by " + j); } } if (divis) { found = true; } } return i; } this will do the job :)
wait, I don't get it, what am I doing wrong. You added in more lines, but my logic should work too right?
what happens in your code is that if 1 of the factors matches it will print it out as a result rather than if all of the numbers are factors
also, you can modify your code like this :- ``` int x; int y; for (x = 2520; x >= 2520; x +=10 ) { boolean found = true; for (y = 1; y <= 20; y++) { if (x % y != 0) { found = false; } } if(found) { System.out.println ("The numer is"+x); } } ```
this works as well, you can break after printing if u want..
I could be off base here, but in this case it makes more sense to me to use a while loop than a for loop. You would still iterate your looping variable by 1 on each loop, but the while would be true only once all of the numbers validate. I don't know what language you are using so I'll use pseudocode - (the following will probably NOT WORK but should give you an idea of the structure) while(something = false) { for (i = 0; i < 20; i++) // or whatever is the proper syntax to loop to 20 { if (yourNum % = 0); { countVar +=1; // this is to keep track of how many numbers between 1 and 20 // work. once if countVar reaches 20, they ALL work. } if (countVar = 20) { lowestCommonDenom = i; something = true; // which terminates your loop } } } This may not be the most elegant way to do it, and I am sure you will have to toy with the syntax a bit, but it should work. Forgive me if somewhere it says what language you are using, I am new to this forum so I don't really know where to look. Below I will explain why this works. The while loop keeps looping until you tell it to stop, which you only do after you have found the number. The for loop inside then counts to 20 and tries each number between 1 and 20. For each number in the for loop, it adds 1 if it works (divides without remainder). So if 10 of the 20 numbers work, by the time it is done the variable's value is 10. If 15 of them work, it's 15 and so on. So if the number in the prior step ever reaches 20, that means that 20 out of 20 (or all of them) worked and we have found our number. We tell the loop that it is ready to stop looping and return the number we have found. Hope this helps.
I would like to clarify that using my method you will still have a for loop, but when I said to use a while loop instead of a for loop I was referring to the outermost loop.
I actually just tested this in C# and can confirm it works. However, as I said earlier, it may not be the most elegant way of doing it because it took my computer about 3 minutes to process it and I have a relatively high end computer. Feel free to tweak as needed. First, you need these variables: bool foundLCD = false; int countVar = 0, LCD = 0, maxNum = 20; Now, here is the method, and you can call it whatever you like. while (foundLCD == false) { LCD += 1; for (int i = 1; i <= maxNum; i++) { if (LCD % i == 0) { countVar += 1; } if (i == maxNum && countVar < maxNum) { countVar = 0; } } if (countVar == maxNum) { foundLCD = true; MessageBox.Show(LCD.ToString()); } } I used a variable (maxNum) instead of 20 because that way if you want to try a different number you can just change the value of maxNum.
Join our real-time social learning platform and learn together with your friends!