Skip to content

Instantly share code, notes, and snippets.

@need4spd
Created November 29, 2012 00:47
[오일러프로젝트] 23번문제
use strict;
use warnings;
use Math::Complex;
use List::Util qw (sum);
sub getDivisors {
my ($num) = @_;
my %divisors_hash=(); #키가 약수
my $l=sqrt($num)+1;
foreach my $n ((1..$l)) {
if ($num % $n == 0) {
unless(exists ($divisors_hash{$num / $n})) {
$divisors_hash{$num / $n}=1;
}
unless(exists ($divisors_hash{$n})) {
$divisors_hash{$n}=1;
}
}
}
#print "num : $num, divisors : @{keys %divisors_hash} \n";
my @divisors=();
foreach my $key ( keys %divisors_hash ) {
unless ($key == $num) {
push @divisors, $key;
}
}
print "num : $num, divisors : @divisors \n";
return @divisors;
}
sub isAbundant {
my ($num) = @_;
my @divisors = getDivisors($num);
my $total = sum @divisors;
if ($total > $num) {
#print "num : $num, total : $total \n";
return 1;
} else {
return 0;
}
}
my $limit = 28123;
my @abundantList=();
foreach (12..$limit-1) {
if (isAbundant($_)) {
push @abundantList, $_;
}
}
#print "a : @abundantList";
my %sumOfAbundant=();
foreach my $i (@abundantList) {
foreach my $j (@abundantList) {
my $t=$i+$j;
if ($t < $limit and (!exists ($sumOfAbundant{$t}))) {
$sumOfAbundant{$t}=1;
}
}
}
#print "sum_of_abundant : @{keys %sumOfAbundant}";
my $result = 0;
foreach (keys %sumOfAbundant) {
$result += $_;
}
$result = sum (1..$limit-1) - $result;
print"total : $result";
# xinuguru님의 function
def getDivisors(N):
divisors = []
for i in range(1, int(N**0.5)+1):
if N % i == 0:
divisors += [i, N/i]
divisors.remove(N)
divisors.sort()
if N==110:
print("divisros : {0}".format(divisors))
return sorted(list(set(divisors)))
def isAbundant(N):
divisors = getDivisors(N)
total = sum(divisors)
#if N < total:
#print(N)
#print("N : {0}, divsors : {1}".format(N, divisors))
if N < total:
return True
else:
return False
limit = 28123
abundantList = [n for n in range(12, limit) if isAbundant(n)]
#print (abundantList)
sumOfAbundant = set()
for i in abundantList:
for j in abundantList:
if i+j < limit:
sumOfAbundant.add(i+j)
#a=sum(range(1,limit))
#b=sum(sumOfAbundant)
#print ("a : {0}, b : {1}".format(a,b))
total = sum(range(1,limit)) - sum(sumOfAbundant)
print(total)
require 'set'
def getDivisors(num)
divisors=Set.new
l=Math.sqrt(num).to_i
for i in (1..l+1)
if num % i == 0
divisors.add(i)
divisors.add(num/i)
end
end
divisors.delete(num)
divisors = divisors.to_a
divisors.sort!
if num==110
puts "divisors : #{divisors}"
end
return divisors
end
def isAbundant(num)
divisors = getDivisors(num)
total = divisors.inject(:+)
#puts "num : #{num}, total : #{total}"
if num < total
#puts num
end
if num < total
return true
else
return false
end
end
limit = 28123
abundantList = (12..limit).to_a.select{|v| isAbundant(v)}
#puts "list : #{abundantList}"
sumOfAbundant = Set.new
for i in abundantList
for j in abundantList
if i+j < limit
sumOfAbundant.add(i+j)
end
end
end
#a=(1..limit-1).to_a.inject(:+)
#b=sumOfAbundant.inject(:+)
#puts "a : #{a}, b : #{b}"
total = (1..limit-1).to_a.inject(:+) - sumOfAbundant.inject(:+)
puts total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment