Solution to Project Euler 5


Improve your writing skills in 5 minutes a day with the Daily Writing Tips email newsletter.

Problem 5 was relatively easier than the ones we saw so far. Here’s the description:

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 positive number that is evenly divisible by all of the numbers from 1 to 20?

As usual, brute force for the win:

#include <stdio.h>

int main(){
  int i,j,counter;
  for (i=10;i<1000000000;i++){
    counter=0;
    for (j=11;j<21;j++){
    if (i%j==0)
      counter++;
    }
    if (counter==10){
      printf("%dn",i);
      break;
    }
  }
        return 0;
}

Notice that if the number is evenly divisible by all numbers from 11 to 20 it’s also divisible by all numbers from 2 to 10.

One thought on “Solution to Project Euler 5

  1. Subhaprasad

    I don’t have much experience in programming. I just had a doubt whether i can write i<1000000000 as i is an integer. Thank you.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *