Project Euler-5

Problem:

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?



Though twenty is not too large, the int will exceed range. As a result, I replace it with long long


#include <stdio.h>
#include <iostream>

using namespace std;

#define LEN 30

long long num[LEN];

void init() {
	for(long long i = 2; i < LEN; i++) {
		num[i] = i;
	}
}

long long gcd(long long a, long long b) {
	long long c;
	while(b) {
		c = a%b;
		a = b;
		b = c;
	}
	return a;
}

int main() {
	init();
	long long ans = 2;
	for(int i = 3; i < 21; i++) {
		ans = ans*num[i]/gcd(ans, num[i]);
	}
	cout<< ans<< endl;
	return 0;
}


你可能感兴趣的:(Project Euler-5)