오일러프로젝트
[오일러프로젝트] 20번문제
용식
2012. 2. 21. 13:47
n! 이라는 표기법은 n × (n − 1) × ... × 3 × 2 × 1을 뜻합니다.
예를 들자면 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800 이 되는데,
여기서 10!의 각 자리수를 더해 보면 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27 입니다.
100! 의 자리수를 모두 더하면 얼마입니까?
간만에 노멀한 문제. 씐나!
Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
a=math.factorial(100) | |
a=str(int(a)) | |
l=list() | |
l+=a | |
print(sum([int(i) for i in l])) |
Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a=(1..100).inject(:*) | |
l=a.to_s.split(//) | |
r = l.inject{|sum,x| sum.to_i + x.to_i} | |
p r |
Perl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use bigint; | |
my $factorial_result = 1; | |
foreach ((1..100)) { | |
$factorial_result = $factorial_result * $_; | |
} | |
my @l = split //, $factorial_result; | |
my $r; | |
foreach (@l) { | |
$r+=$_; | |
} | |
print $r; |
외국 오일러프로젝트의 다른 개발자의 Perl코드..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Math::BigInt; | |
my $sum = 0; | |
map { $sum += $_ } split '', Math::BigInt->new( 100 )->bfac()->bstr(); | |
print "Sum is $sum\n"; |