python - My ProjectEuler implementation is much too slow -


this question has answer here:

these problem:

2520 smallest number can divided each of numbers 1 10 without remainder. smallest positive number evenly divisible of numbers 1 20?

these solution, takes lot of time!

x = 0 check = [11, 13, 14, 16, 17, 18, 19, 20]  while true:     x += 1     if all(x % j == 0 j in check):         print "the number is", x         break     print x 

how improve it? counter main problem? or while?
edit: i've seen there lot of differente solutions, should check every single number 1 , without function lcm?

this implementation c++ program.

instead of number++, go number += 2, why? because odd number never divisible 2 therefore reducing search space o(n) o(n/2). there 1 more way reduce space search, shifting code number += 20 (limit), because know number of multiple 20 divisible numbers 1-20 (call advanced maths :p).

next start checking limit 20 because there more change of failing @ %limit @ 2, 3, 4, 5 etc. // smaller numbers. therefore reducing computational clock cycles bit.

#include<fstream> #include<iostream> using namespace std;   int main ( ) {     int limit = 20;      bool found;     int number = 2; // or number = limit;       {          found = true;         ( int i=limit; i<=1 && found; --i ) {             if ( number%i != 0 ) found = false;         }          number += 2; // or number += limit;     } while ( !found );      cout << "smallest number divisible 1 " << limit << " is: " << number -= 2 /* or number -= limit; since added 1 more time @the end */ << endl; } 

i hope helps :)


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -