Skip to content

Instantly share code, notes, and snippets.

View gavinsykes's full-sized avatar

Gavin Sykes gavinsykes

View GitHub Profile
<?php
function euler_1($n) {
$result = 0;
$x = $n/3;
$y = $n/5;
for ($i = 1;$i<$x;$i++) {
$result += 3 * $i;
}
for ($i = 1;$i<$y;$i++) {
// If statement to make sure we haven't already added the number as a multiple of 3
<?php
/*
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
O,E,O,O,E,O,O,E,O,O,
# The sum of the squares of the first ten natural numbers is,
# 1^2 + 2^2 + ... + 10^2 = 385
#
# The square of the sum of the first ten natural numbers is,
# (1 + 2 + ... + 10)^2 = 55^2 = 3025
# Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640.
# Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
# n! means n x (n - 1) x ... x 3 x 2 x 1
# For example, 10! = 10 x 9 x ... x 3 x 2 x 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
# Find the sum of the digits in the number 100!
def euler_20(n):
result = 0
for i in str(factorial(n)):
result += int(i)
# I have decided to tackle this one in Python rather than PHP as I could quite happily do this in PHP but the point of doing these exercises is to expand my knowledge of multiple languages rather than just "solve them".
#
# Going forward it will likely be a mix of languages, I imagine it will pimarily be PHP and Python.
#
# The prime factors of 13195 are 5, 7, 13 and 29.
#
# What is the largest prime factor of the number 600851475143?
import math
# A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.
#
# Find the largest palindrome made from the product of two 3-digit numbers.
def largest_palindrome_product(n):
result = 0
for i in range(n):
for j in range(n):
if(is_palindrome(i*j) and i*j > result):
result = i*j
# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
#
# What is the 10001st prime number?
import math
import time
def is_prime(n):
# Check if it's divisible by 2
if(not(n & 1)):
# The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
#
# 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111217223831136222989342338030813533627661428280644448664523874930358907296290491560440772390713810515859307960866701724271218839987979087922749219016997208880937766572733300105336788122023542180975125454059475224352584907711670556013604839586446706324415722155397536978179778461740649551492908625693219784686224828397224137565705605749026140797296865241453510047482166370484403199890008895243450658541227588666881164271714799244429282308634656748139191231628245861786645835912456652947654568284891288314260769004224219022671055626321111109370544217506941658960408071984038509624554443629812309878799272442849091888458015616609791913387549920052406368991256071760605886116467
function euler_9($n) {
$a = $b = $c = 1;
for ($a = 1;$a <= $n-2;$a++) {
for ($b = 1; $b <= $n-1-$a;$b++) {
$c = $n - $a - $b;
if (pythag_trip($a,$b,$c)) {
return $a . ', ' . $b . ' and ' . $c . ' make ' . $a*$b*$c . '.';
}
}
}
@gavinsykes
gavinsykes / euler_problem_15.c
Last active December 9, 2019 22:21
Project Euler Problem 15 in C
#include <stdio.h>
int nCr(int n,int r);
int factorial(int n);
int main() {
int n;
scanf("Please enter a number here: %d",&n);
printf("Here is your answer: %d\n",nCr(2*n,n));
return 0;