Solved ProjectEuler\020: php, ruby
This commit is contained in:
18
ProjectEuler/020/desc.yml
Normal file
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
|
15
ProjectEuler/020/solve.php
Normal file
15
ProjectEuler/020/solve.php
Normal file
@ -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
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
|
Reference in New Issue
Block a user