Sunday, March 15, 2015

(C Language) (ENG) Smallest Multiple - Project Euler Problem 5

PROBLEM

For example 2520 is the smallest positive number which can be divided by integers from 1 to 10(included) with remainder '0'.

What is the smallest positive number which can be divided by integers from 1 to 20(included) then?

This c problem is taken by me from: (https://projecteuler.net/problem=5)

SAMPLE RUN:

In addition to problem, i printed result on the screen.




SOLUTION

You can copy codes below to your compiler to execute.




#include <stdio.h>

int main(void) //main function.
{

int spn = 20; //smallest positive number that mentioned.
int flag = 0; //we will use it to get out from loop or continue.
int counter = 0; //this counter will show program how many times spn was divided with remainder 0.

while(flag == 0)
{
spn++; //this code will update spn to check next value.
for (int k = 1; k <= 20; k++) //this loop will find if spn is divisible for all integers 20 >= x > 1 with 0 remainder.
{
if (spn % k == 0)
counter++;
else break;
}
if (counter == 20) //if it is true, it means that we find spn that I searched.
flag = 1;
counter = 0; //this code will turn counter to 0 to use it again.
}

printf("smallest positive number is %d", spn);

return 0;
}

No comments:

Post a Comment