Advertisement
banovski

Project Euler, Problem #5, C

Dec 10th, 2021 (edited)
1,528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* The problem: 2520 is the smallest number that can be divided by
  4. each of the numbers from 1 to 10 without any remainder. What is the
  5. smallest positive number that is evenly divisible by all of the
  6. numbers from 1 to 20? */
  7.  
  8. /* Since non-prime numbers in the range of 11 through 20 are all
  9. divisible by a number in the range of 2 through 10, only numbers in
  10. the former range are checked for being factors. Thus, the product of
  11. these numbers should the maximum checked value. */
  12.  
  13. int main()
  14. {
  15.      long int product = 1;
  16.      int i;
  17.      for(i = 11; i < 21; i++)
  18.           product *= i;
  19.  
  20. /* As the biggest factor is 20 and the check starts with 20, the
  21. step should also be 20. */
  22.  
  23.      long int checked = 20;
  24.      int interrupted;
  25.      while(checked <= product)
  26.      {
  27.           interrupted = 0;
  28.           for(i = 20; i > 10; i--)
  29.                if(checked % i != 0)
  30.                {
  31.                     interrupted = 1;
  32.                     break;
  33.                }
  34.           if(interrupted != 1)
  35.           {
  36.                printf("%ld\n", checked);
  37.                break;
  38.           }
  39.           checked += 20;
  40.      }
  41.      return(0);
  42. }
  43.  
  44. /* 232792560 */
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement