Project Euler Solutions Thread 05-06-2014, 05:05 AM
#1
Might as well start something fun.
A few starting solutions I've written for Project Euler problems. I may add some more later. Feel free to post your solutions if you want.
I've solved over 70 problems so far, but some others may not be as far so keep in mind to use spoilers, and provide explanations for the more difficult problems if the code seems cryptic.
Problem 1: C++
Problem 2: C++
Problem 3: C++
Problem 4: C++
Problem 5: C++
Problem 6: C
A few starting solutions I've written for Project Euler problems. I may add some more later. Feel free to post your solutions if you want.
I've solved over 70 problems so far, but some others may not be as far so keep in mind to use spoilers, and provide explanations for the more difficult problems if the code seems cryptic.Problem 1: C++
Spoiler:
Code:
#include <iostream>
int main()
{
const int max_n = 1000;
int j = 0, count = 0;
for (int i = 3; i < max_n; i += 3)
{
if (i > j && (j += 5) < max_n) count += j;
if (i != j) count += i;
}
std::cout << count << std::endl;
}Problem 2: C++
Spoiler:
Code:
#include <iostream>
int main()
{
int sum = 0, i = 1;
const int max_n = 4000000;
int fib[2] = {1, 2};
do
{
sum += fib[i] & 1 ? 0 : fib[i];
fib[i ^= 1] = fib[0] + fib[1];
} while (fib[i] < max_n);
std::cout << sum << std::endl;
}Problem 3: C++
Spoiler:
Code:
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
/*
* What is the largest prime factor
* of the number 600851475143?
*/
void prime_atkin(const int max_n, std::vector<int> &primes)
{
std::vector<bool> p(max_n, false);
int sqrt_limit(static_cast<int>(std::sqrt(max_n)));
for (int x(1); x <= sqrt_limit; x++)
{
const int x2(x * x);
for (int y(1); y <= sqrt_limit; y++)
{
const int y2(y * y);
int n = (4 * x2) + (y2);
if (n <= max_n && (n % 12 == 1 || n % 12 == 5))
p[n] = p[n] ^ true;
n = (3 * x2) + (y2);
if (n <= max_n && n % 12 == 7)
p[n] = p[n] ^ true;
n = (3 * x2) - (y2);
if (x > y && n <= max_n && n % 12 == 11)
p[n] = p[n] ^ true;
}
}
primes.push_back(2);
primes.push_back(3);
int a(5);
for (; a <= sqrt_limit; a += 2)
{
if (p[a])
{
const int a2(a * a);
for (int i(a2); i < max_n; i += a2) p[i] = false;
primes.push_back(a);
}
}
for (; a < max_n; a += 2) if (p[a]) primes.push_back(a);
}
template <class C>
class reverse_wrapper
{
C &container;
public:
reverse_wrapper(C &c) : container(c) { }
decltype(container.rbegin()) begin() { return container.rbegin(); }
decltype(container.rend()) end() { return container.rend(); }
};
int main()
{
uint64_t target = 600851475143L;
int x = (int)sqrt(target);
std::vector<int> primes; prime_atkin(x, primes);
for (const auto &it : reverse_wrapper<std::vector<int>>(primes))
{
if (target % it == 0)
{
std::cout << it << std::endl;
return 0;
}
}
}Problem 4: C++
Spoiler:
Code:
#include <iostream>
#include <set>
#define P 100000
bool is_palindrome(int n)
{
int i = n;
int num = 0;
int p = P;
do
{
num += (i % 10) * p;
i /= 10; p /= 10;
}
while (i > 0);
return num == n;
}
int main()
{
std::set<int> palindromes;
int product;
for (int i = 999; i > 100; --i)
for (int j = i - 1; j > 100; --j)
if (is_palindrome(product = i * j))
palindromes.insert(palindromes.begin(), product);
auto it = palindromes.end(); --it;
std::cout << *it << std::endl;
}Problem 5: C++
Spoiler:
Code:
#include <iostream>
#include <algorithm>
int main()
{
const int div_test[] = { 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 9 };
size_t len = sizeof(div_test) / sizeof(div_test[0]);
for (int i = 2520; ; i += 20)
{
if (std::all_of(div_test, div_test + len,
[&i](int n) { return i % n == 0; }))
{
std::cout << i << std::endl;
return 0;
}
}
}Problem 6: C
Spoiler:
Code:
#include <stdio.h>
#define SUM_SQUARES 0
#define SUM_SQUARED 1
int main()
{
const int max_n = 100;
int sum[2] = {0, 0};
for (int i = 1; i <= max_n; ++i)
{
sum[SUM_SQUARES] += i * i;
sum[SUM_SQUARED] += i;
}
sum[SUM_SQUARED] *= sum[SUM_SQUARED];
printf("%d\n", sum[SUM_SQUARED] - sum[SUM_SQUARES]);
}
(This post was last modified: 05-06-2014, 05:18 AM by 0xDEAD10CC.)




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)




















