2 ≤ a ≤ 5 이고 2 ≤ b ≤ 5인 두 정수 a, b로 만들 수 있는 ab의 모든 조합을 구하면 다음과 같습니다.
22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125
여기서 중복된 것을 빼고 크기 순으로 나열하면 아래와 같은 15개의 숫자가 됩니다.
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
그러면, 2 ≤ a ≤ 100 이고 2 ≤ b ≤ 100인 a, b를 가지고 만들 수 있는 ab는 중복을 제외하면 모두 몇 개입니까?
문제는 평이한 수준이나
perld에서 Set 사용을 위한 cpan 이용
그리고 큰 수 표현을 위한 BigInt를 사용하면서 좀 헤맸음..
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
r=set() | |
for a in range(2,101): | |
for b in range(2,101): | |
r.add(a**b) | |
print(r, len(r)) |
Ruby
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
require 'set' | |
r=Set.new | |
(2..100).each do |a| | |
(2..100).each do |b| | |
r.add(a**b) | |
end | |
end | |
puts r.length |
Perl
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
use strict; | |
use warnings; | |
use Set::Light; | |
use Math::BigInt; | |
my $r = Set::Light->new(); | |
foreach my $a ((2..100)) { | |
foreach my $b ((2..100)) { | |
my $temp = Math::BigInt->new($a); | |
$temp->bpow($b); | |
print "$temp \n"; | |
$r->insert($temp); | |
} | |
} | |
my $size = $r->size(); | |
print "$size"; |