Created
January 19, 2018 04:49
-
-
Save xtetsuji/20b1ec8adac7d6392e8daf6cab9856d0 to your computer and use it in GitHub Desktop.
How many kinds of numbers does n*n multiplication table have? What is its order?
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 Memoize qw(memoize); | |
memoize("get_variation_hash"); | |
for my $n (9..1_000_000) { | |
my $variation = get_variation_hash($n); | |
my $variation_count = scalar keys %$variation; | |
#printf "%4d: %4d\n", $n, $variation_count; | |
#printf "%4d: %4d (%.4f)\n", $n, $variation_count, $variation_count/log($n); | |
#printf "%4d: %4d (%.4f)\n", $n, $variation_count, $variation_count/($n*$n*log($n)); | |
printf "%4d: %4d (%.4f)\n", $n, $variation_count, $variation_count/($n*$n); | |
} | |
sub get_variation_hash { | |
my $n = shift; | |
if ( $n < 1 || $n =~ /\D/ ) { | |
die "n は 1 以上の自然数である必要があります"; | |
} elsif ( $n == 1 ) { | |
return +{ 1 => 1 }; # 1 という数字がある(フラグ1)だけ | |
} | |
my $variation = \%{ get_variation_hash($n-1) }; | |
for my $i (1..$n) { | |
$variation->{ $i * $n }++; | |
} | |
return $variation; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment