ProjectEuler
// 001
1 #include2 3 using namespace std; 4 5 int main() 6 { 7 int sum = 0; 8 for (int i = 3; i < 1000; ++ i) { 9 if (i % 3 == 0 || i % 5 == 0) {10 sum += i;11 }12 }13 cout << sum << endl;14 15 return 0;16 }
//002
1 #include2 3 using namespace std; 4 5 int main() 6 { 7 int num1 = 1, num2 = 2; 8 int temp = 0; 9 int sum = 0;10 while (num2 < 4000001) {11 if (num2 % 2 == 0) {12 sum += num2;13 }14 temp = num1 + num2;15 num1 = num2;16 num2 = temp;17 }18 cout << sum << endl;19 20 return 0;21 }
// 003
1 #include2 #include 3 4 using namespace std; 5 6 bool isPrime(int); 7 8 int main() 9 {10 double sq = sqrt(600851475143.0);11 int num = sq;12 for (int i = num; i > 1; -- i) {13 if (600851475143 % i == 0 && isPrime(i)) {14 cout << i << endl;15 break;16 }17 }18 19 return 0;20 }21 22 bool isPrime(int num)23 {24 if (num < 2) return false;25 for (int i = 2; i <= sqrt((double)num); ++ i) {26 if (num % i == 0) {27 return false;28 }29 }30 return true;31 }