~xenrox/euler

c6f403cb68e1830a661c2728340d93778027c05e — Thorben Günther 1 year, 11 months ago 57620e6 master
First 4 solutions
4 files changed, 86 insertions(+), 0 deletions(-)

A 1.py
A 2.cpp
A 3.cpp
A 4.cpp
A 1.py => 1.py +7 -0
@@ 0,0 1,7 @@
#!/usr/bin/python

sum = 0
for i in range(0, 1000):
    if i % 3 == 0 or i % 5 == 0:
        sum += i
print(sum)

A 2.cpp => 2.cpp +33 -0
@@ 0,0 1,33 @@
#include <iostream>
#include <vector>

const int GOAL = 4000000;

void calcFib(std::vector<int> &fib) {
  int next = fib.end()[-1] + fib.end()[-2];
  if (next <= GOAL) {
    fib.push_back(next);
    calcFib(fib);
  }
}

int calcEvenSum(const std::vector<int> &fib) {
  int sum = 0;
  for (const auto &value : fib) {
    if (value % 2 == 0) {
      sum += value;
    }
  }
  return sum;
}

int main(int argc, char *argv[]) {
  std::vector<int> fib;
  fib.push_back(1);
  fib.push_back(2);

  calcFib(fib);
  std::cout << calcEvenSum(fib) << std::endl;

  return 0;
}

A 3.cpp => 3.cpp +17 -0
@@ 0,0 1,17 @@
#include <iostream>

const long NUM = 600851475143;

int largestPrimeFactor(long number) {
  for (int factor = 2; factor < number; factor++) {
    if (number % factor == 0) {
      number /= factor;
    }
  }
  return number;
}

int main(int argc, char *argv[]) {
  std::cout << largestPrimeFactor(NUM) << std::endl;
  return 0;
}

A 4.cpp => 4.cpp +29 -0
@@ 0,0 1,29 @@
#include <iostream>

bool checkPalindrome(int n) {
  int reverse = 0;
  int original = n;
  while (n != 0) {
    int rest;
    reverse *= 10;
    rest = n % 10;
    n /= 10;
    reverse += rest;
  }
  return reverse == original;
}

int main(int argc, char *argv[]) {
  int product;
  int biggest = 1;
  for (int i = 1; i < 1000; i++) {
    for (int j = 1; j < 1000; j++) {
      product = i * j;
      if (checkPalindrome(product) && product > biggest) {
        biggest = product;
      }
    }
  }
  std::cout << biggest << std::endl;
  return 0;
}