Euler 005 c++ Solution

Smallest multiple

Problem

https://projecteuler.net/problem=5

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?

Answer: 232792560

Solution

euler005.cpp

#include <iostream>

int prob005_brute_force(int max)
{
  uint32_t answer = 0;
  uint32_t test = max;
  bool check = false;
  while( !check ){
    check = true;
    for( uint32_t i = max ; i && check ; --i){
      check &= (0 == (test%i));
    }
    if( !check ){
      test += 20;
    }
  }
  answer = test;
  return answer;
}

int main( int argc , char* argv[])
{
  std::cout << "Answer: " << prob005_brute_force(20) << std::endl;
}

See Also

# cpp go java php ruby rust javascript
1
2
3  
4    
5    
6      
7          
8          
9          
10          
11          
12          
# cpp ruby
13
14
15
16
17
18
19
20  
21  
22  
23  
24