From 9dd9d918bfbbb4db63c51b892bdce71bf2bd94ab Mon Sep 17 00:00:00 2001 From: FuryFire Date: Tue, 6 Mar 2012 14:21:54 +0100 Subject: [PATCH] Solved a few more problems 003: php 005: php, ruby 006: php, ruby, c --- ProjectEuler/003/desc.yml | 15 +++++++++++++++ ProjectEuler/003/solve.php | 33 +++++++++++++++++++++++++++++++++ ProjectEuler/005/desc.yml | 17 +++++++++++++++++ ProjectEuler/005/solve.php | 12 ++++++++++++ ProjectEuler/005/solve.rb | 10 ++++++++++ ProjectEuler/006/desc.yml | 24 ++++++++++++++++++++++++ ProjectEuler/006/solve.c | 19 +++++++++++++++++++ ProjectEuler/006/solve.php | 9 +++++++++ ProjectEuler/006/solve.rb | 7 +++++++ 9 files changed, 146 insertions(+) create mode 100644 ProjectEuler/003/desc.yml create mode 100644 ProjectEuler/003/solve.php create mode 100644 ProjectEuler/005/desc.yml create mode 100644 ProjectEuler/005/solve.php create mode 100644 ProjectEuler/005/solve.rb create mode 100644 ProjectEuler/006/desc.yml create mode 100644 ProjectEuler/006/solve.c create mode 100644 ProjectEuler/006/solve.php create mode 100644 ProjectEuler/006/solve.rb diff --git a/ProjectEuler/003/desc.yml b/ProjectEuler/003/desc.yml new file mode 100644 index 0000000..5f9d3e0 --- /dev/null +++ b/ProjectEuler/003/desc.yml @@ -0,0 +1,15 @@ +title: Find the largest prime factor of a composite number. +url: http://projecteuler.net/problem=3 + +desc: | + The prime factors of 13195 are 5, 7, 13 and 29. + What is the largest prime factor of the number 600851475143 ? + +solution: | + Use factorization to find the largest prime factor + +solutions: + solve.php: + desc: Basic solution + language: php + diff --git a/ProjectEuler/003/solve.php b/ProjectEuler/003/solve.php new file mode 100644 index 0000000..aca79fa --- /dev/null +++ b/ProjectEuler/003/solve.php @@ -0,0 +1,33 @@ + [5, 3, 3, 2, 2, 2] + static $aFactors = array(); + if (2 > $num = abs($num)) + return array(); // negatives, 1, 0 + + if ($aFactors[$key = "x$num"]) { // handles doubles + // Been there, done that + if (($factor = $aFactors[$key]) == $num) + return array($num); + return array_merge(factorize($num / $factor), array($factor)); + } + + // Find a smallest factor + for ($sqrt = sqrt($num), $factor = 2; $factor <= $sqrt; ++$factor) + if (floor($num / $factor) == $num / $factor) + return array_merge(factorize($num / $factor), array($aFactors[$key] = $factor)); + + return (array($aFactors[$key] = $num)); +} diff --git a/ProjectEuler/005/desc.yml b/ProjectEuler/005/desc.yml new file mode 100644 index 0000000..3fb5d6a --- /dev/null +++ b/ProjectEuler/005/desc.yml @@ -0,0 +1,17 @@ +title: What is the smallest number divisible by each of the numbers 1 to 20? +url: http://projecteuler.net/problem=5 + +desc: | + 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? + +solution: | + See code + +solutions: + solve.php: + desc: Basic solution + language: php + solve.rb: + desc: Basic solution in Ruby + language: ruby diff --git a/ProjectEuler/005/solve.php b/ProjectEuler/005/solve.php new file mode 100644 index 0000000..388c715 --- /dev/null +++ b/ProjectEuler/005/solve.php @@ -0,0 +1,12 @@ + max and sum.to_s.reverse == sum.to_s) + max = sum + end + end +end +print max \ No newline at end of file diff --git a/ProjectEuler/006/desc.yml b/ProjectEuler/006/desc.yml new file mode 100644 index 0000000..77ebe89 --- /dev/null +++ b/ProjectEuler/006/desc.yml @@ -0,0 +1,24 @@ +title: What is the smallest number divisible by each of the numbers 1 to 20? +url: http://projecteuler.net/problem=6 + +desc: | + 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. + +solution: | + See code + +solutions: + solve.php: + desc: Basic solution + language: php + solve.rb: + desc: Basic Ruby solution + language: ruby + solve.rb + desc: ANSI C solution (Tested with TCC) + language: c \ No newline at end of file diff --git a/ProjectEuler/006/solve.c b/ProjectEuler/006/solve.c new file mode 100644 index 0000000..3f510a4 --- /dev/null +++ b/ProjectEuler/006/solve.c @@ -0,0 +1,19 @@ +#include +#include + +int main( ) +{ + int num; + int result; + + int square = 0; + int sum = 0; + + for(num=1; num<=100; num++) { + square += pow(num,2); + sum += num; + + } + result = pow(sum,2) - square; + printf("%d", result); +} \ No newline at end of file diff --git a/ProjectEuler/006/solve.php b/ProjectEuler/006/solve.php new file mode 100644 index 0000000..a347e14 --- /dev/null +++ b/ProjectEuler/006/solve.php @@ -0,0 +1,9 @@ +