Created
November 17, 2017 06:37
-
-
Save xtetsuji/f9f0ada0f6f5b642d745516d43d5e52e to your computer and use it in GitHub Desktop.
Recursive sum of each decimal places. This output is in 9 cycle.
This file contains 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 constant MAX_NUMBER => 1_000_000; | |
use List::Util qw(sum); | |
my %stat; | |
for (my $i = 1; $i < MAX_NUMBER; $i++) { | |
my $recursive_sum_digit = recursive_sum_digit($i); | |
print "$i => $recursive_sum_digit\n"; | |
$stat{$recursive_sum_digit}++; | |
} | |
# stat | |
printf qq{%d:%d(%2.2f%%) }, $_, $stat{$_}, $stat{$_}/MAX_NUMBER for 1..9; | |
print "\b\n"; | |
sub recursive_sum_digit { | |
my $number = sum_digit(shift); | |
while ($number >= 10) { | |
$number = sum_digit($number); | |
} | |
return $number; | |
} | |
sub sum_digit { | |
my $number = shift; | |
#print "number=$number\n"; | |
return sum split //, $number; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment