Project Euler: http://projecteuler.net/
This commit is contained in:
56
ProjectEuler/p005_SmallestMultiple.cpp
Normal file
56
ProjectEuler/p005_SmallestMultiple.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
/* Check cf5-opt.vim defs.
|
||||
VIM: let g:lcppflags="-std=c++11 -O2 -pthread"
|
||||
VIM: let g:wcppflags="/O2 /EHsc /DWIN32"
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
|
||||
/*
|
||||
Smallest multiple
|
||||
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?
|
||||
|
||||
Solution:
|
||||
232792560
|
||||
*/
|
||||
|
||||
long long lacking_factor( long long n, long long m )
|
||||
{
|
||||
long long h = (long long)sqrt(double(n));
|
||||
long long l = 1;
|
||||
for ( long long i = 2; i <= h; )
|
||||
if ( n%i )
|
||||
++i;
|
||||
else
|
||||
{
|
||||
n/=i;
|
||||
h = (long long)sqrt(double(n));
|
||||
if ( m%i )
|
||||
l*=i;
|
||||
else
|
||||
m/=i;
|
||||
}
|
||||
|
||||
if ( m%n )
|
||||
l *= n;
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
int main ( void )
|
||||
{
|
||||
long long m = 1;
|
||||
for ( int i = 2; i <= 20; ++i )
|
||||
{
|
||||
m *= lacking_factor( i, m );
|
||||
//std::cout << i << " " << m << std::endl;
|
||||
}
|
||||
|
||||
std::cout << m << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user