Solved ProjectEuler\020: php, ruby

This commit is contained in:
FuryFire 2012-03-09 13:56:02 +01:00
parent e689e807da
commit 36c31696a9
3 changed files with 49 additions and 0 deletions

18
ProjectEuler/020/desc.yml Normal file

@ -0,0 +1,18 @@
title: Find the sum of digits in 100!
url: http://projecteuler.net/problem=20
desc: |
n! means n * (n - 1) * ... 3 * 2 * 1
For example, 10! = 10 * 9 * ... * 3 * 2 * 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!
solution: Bruteforce
solutions:
solve.php:
desc: Basic solution
language: php
solve.rb:
desc: Basic solution
language: ruby

@ -0,0 +1,15 @@
<?php
define('FACTORIAL',100);
function factorial($num) {
if($num == 0)
return 1;
else
return bcmul($num,factorial(bcsub($num,1)));
}
$sum = 0;
$fac = factorial(FACTORIAL);
for($c = 0; $c < strlen($fac); $c++) {
$sum += $fac[$c];
}
echo $sum;

16
ProjectEuler/020/solve.rb Normal file

@ -0,0 +1,16 @@
FACTORIAL = 100
def factorial(num)
if(num == 0)
return 1
else
return num * factorial(num - 1)
end
end
number = factorial(FACTORIAL)
sum = 0
number.to_s.each_char do |c|
sum += c.to_i
end
puts sum