solution 21
ans: 40284
function solution_21() #这个算法太慢,需要优化
d(a) = reduce(+,filter(x->a%x==0,1:a-1);init=0)
iter = function iter(a,res)
if a == 10000
res
else
b = d(a)
if a == d(b)
iter(a+1,res+a)
else
iter(a+1,res)
end
end
end
iter(1,0)
end
solution 22
ans 871198282
function solution_22() # 这里有个笔记,最好用Iterators.drop而不是Iterators.rest,
在这种尾递归里rest容易出错
num_of_char(ch) = Int(lowercase(ch)) - Int('a') + 1
name_score(name,index) = reduce(+,map(num_of_char,collect(name));init=0) * index
names = sort(split(replace(read(open("names.txt"),String),"\""=>""),","))
iter = function iter(n,names,res=name_score(first(names),n))
if n == length(names)
res
else
iter(n+1,Iterators.drop(names,1),res + name_score(first(Iterators.drop(names,1)),n+1))
end
end
iter(1,names)
end
solution 25
ans 4782
function solution_25()
first(first(Iterators.filter(has_1000_length,collect(Iterators.enumerate(Iterators.take(fib,10000))))))
end
Or
@thread_as(Iterators.take(fib,10000), _ ,
Iterators.enumerate(_),
collect(_),
Iterators.filter(has_1000_length,_),
first(_),
first(_))
好了,其他我实在没能力写下去了,学习是个慢慢积累的过程,我去积累知识了