Solved a few more problems

003: php
005: php, ruby
006: php, ruby, c
This commit is contained in:
FuryFire
2012-03-06 14:21:54 +01:00
parent 15e919438d
commit 9dd9d918bf
9 changed files with 146 additions and 0 deletions

15
ProjectEuler/003/desc.yml Normal file
View File

@ -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

View File

@ -0,0 +1,33 @@
<?php
$number = 600851475143;
echo max(factorize($number));
/**
* Returns a sorted array of the prime factorization of $num
* @staticvar array $aFactors
* @param type int Number to factorize
* @return type array Prime factors
*/
function factorize($num) {
// Returns a sorted array of the prime factorization of $num
// Caches prior results. Returns empty array for |$num|<2
// eg. factorize(360) => [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));
}

17
ProjectEuler/005/desc.yml Normal file
View File

@ -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

View File

@ -0,0 +1,12 @@
<?php
for($i=20;true;$i+=20) {
$div = 19;
while(!($i % $div)) {
$div--;
if($div == 0) {
echo $i;
die;
}
}
}

10
ProjectEuler/005/solve.rb Normal file
View File

@ -0,0 +1,10 @@
max = 0;
(100..1000).each do |num1|
(100..1000).each do |num2|
sum = num1 * num2
if( sum > max and sum.to_s.reverse == sum.to_s)
max = sum
end
end
end
print max

24
ProjectEuler/006/desc.yml Normal file
View File

@ -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

19
ProjectEuler/006/solve.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdio.h>
#include <math.h>
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);
}

View File

@ -0,0 +1,9 @@
<?php
$square =0;
$sum =0;
for($num=1;$num<101;$num++) {
$square += pow($num,2);
$sum += $num;
}
echo pow($sum,2) - $square;

View File

@ -0,0 +1,7 @@
square = 0
sum = 0
(1..100).each do |num|
square += num**2;
sum += num;
end
puts sum**2 - square;