New course! Every coder should learn Generative AI!
Try a free lesson0
Can you solve this using C/C++ only! Give it a try!!
2520 is the smallest number that can be divided by each of the numbers from 1-10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1-20?
7 Answers
+ 2
int i = 1;
while (i % 2 != 0 || i % 3 != 0 || i % 4 != 0 || i % 5 != 0 || i % 6 != 0 || i % 7 != 0 || i % 8 != 0 || i % 9 != 0 || i % 10 != 0 || i % 11 != 0 || i % 12 != 0 || i % 13 != 0 || i % 14 != 0 || i % 15 != 0 || i % 16 != 0 || i % 17 != 0 || i % 18 != 0 || i % 19 != 0 || i % 20 != 0 ){ i++;
}
Result:-
The smallest number dividable with all number 1-20 is 232792560 Solution took 718,75 ms
+ 2
i hope this isnt your homework
+ 2
https://code.sololearn.com/c9WWJQ8dx9Wb/?ref=app
(note that you don't need to check if a number is divisible by 2-10 if you already know it's divisible by 11-20, also you can increase by steps of 20 if you know that the number has to be divisible by 20)
+ 1
A quick web search results, going for ProjectEuler?
https://www.geeksforgeeks.org/smallest-number-divisible-first-n-numbers/
https://stackoverflow.com/questions/13719768/project-euler-5smallest-positive-number-divisible-by-all-numbers-from-1-to-20
https://answers.yahoo.com/question/index?qid=20100123221401AA7q4lR
+ 1
actualy, there is a bit more complex way, but a lot quicker, since there isnt as much counting
every number above 1 can be expressed as a product of some prime numbers (or that number is a prime itself). With that said, the number we can assemble the number we are looking for from prime numbers by cleverly using this property
So, our number must be divideable by anything from 2 to 20. Lets start from 2. 2 is a prime. Our number must contain at least one 2 as it's prime factor. Next up we have 3, its a prime. So our number must have 2*3. Next up we have 4. 4 is 2*2. We already have one 2, so we need a second one for it to be divideable by 4. That will be 2*2*3. You perform this for every number until you reach 20. If your number (like 6) can be achieved by multiplying primes you already have (2*3), you dont do anything. If not (like 9), you add needed primes (3)
In the end, you will have this:
2*2*2*2*3*3*5*7*11*13*17*19
this will be the result
0
Data actually it is!! 😅
0
oh, it looks like someone already posted about a quicker way