аватар question@mail.ru · 01.01.1970 03:00

The more rational implementation of the enforcement algorithm to find the smallest positive number, which is divided into all numbers from 1 to 20

There is a problem:

2520 is the smallest number that can be divided into each of the numbers from 1 to 10 without a residue.

Find such a smallest positive number that is divided by all numbers from 1 to 20?

the task of this I decided, having written a poorly optimized algorithm for a complete enumeration of options.

  drawculate =  true  max_number =  20  min_number =  1  current_divisible =  2520  count =  0    While  calculate:   for  i  in   range  ( 2 , max_number +  1 ):   if  current_divisible % I ==  0 :  count + =  1   min_number = current_divisible  else :  count =  0    1    if  count == max_number:  classlate =  false    Print  (min_number) 
                            
                            
                        
аватар answer@mail.ru · 01.01.1970 03:00

Generally speaking, you just need to look for the smallest common multiple for all numbers from 1 to 20 :) elementary, no busting, instantly.

I can throw the code on C or C ++, Python I don’t know.

Here is the code - the Euclid algorithm for nod, then NOK ...

  Long Gcd (Long M, Long N)  {  white  (m & amp; & amp; n)  if  (m & lt; n) n %= m;  else  m %= n;   retu  (m ==  0l )? n: m; } long lcm (long m, long n)  {  retu  (m/gcd (m, n)*n; }   int  main ( int  argc, const char * argv [])  { long res =  1 ;   for  ( into  i =  2 ; =  20 ; Res & lt; & lt; endl; }                                       

Latest

Similar