Skip to content

Instantly share code, notes, and snippets.

@jschwinger233
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save jschwinger233/182de4415da9526ae894 to your computer and use it in GitHub Desktop.

Select an option

Save jschwinger233/182de4415da9526ae894 to your computer and use it in GitHub Desktop.
pe3
@time for i = 1:10
factor(600851475143)|>keys|>maximum|>println
end
pe4
function pe4()
for s = 1998:-1:200, i = 999:-1:s-999
pro = string(i * (s-i))
pro == reverse(pro) && return pro
end
end
@time println(pe4())
pe12
function countFactor(n::Int)
count = 1
for i in values(factor(n))
count *= i + 1
end
count
end
function pe12(lim::Int)
i = 8
while countFactor(div(i*(i+1), 2)) < lim
i += 1
end
div(i*(i+1), 2)
end
@time for i = 1:5
pe12(500)|>println
end
pe14
d = Dict{Int, Int}()
d[1] = 1
function PE14(n::Int)
haskey(d, n) && return d[n]
iseven(n) ? (d[n] = PE14(div(n, 2)) + 1) : (d[n] = PE14(3n+1) + 1)
end
function main()
crt = idx = 0
for i = 1:1000000
t = PE14(i)
t > crt && (crt = t; idx = i)
end
idx
end
@time main()|>println
pe15
d = Dict{(Int, Int), Int}()
function rec(m::Int, n::Int)
(m == 0 || n == 0) && return 1
haskey(d, (m, n)) && return d[m, n]
return d[m, n] = rec(m-1, n) + rec(m, n-1)
end
@time rec(20, 20)
pe23
function PE23(lim::Int)
factorsum = ones(Int, lim)
for f = 2:lim
factorsum[2f:f:end] += f
end
p = trues(lim)
abundant = find(factorsum .> [1:lim])
for i in abundant, j in abundant
(i + j > lim) && continue
p[i+j] = false
end
sum(find(p))
end
@time PE23(28123)
pe24
a = [0:9]
perm = zeros(Int, 10)
used = trues(10)
global cnt = 0
function PE24(pos::Int)
if pos == 11
global cnt += 1
cnt == 1000000 && println(perm)
else
for i = 1:10
if used[i]
perm[pos] = a[i]
used[i] = false
pe24(pos + 1)
cnt == 1000000 && return 0
used[i] = true
end
end
end
end
@time PE24(1)
pe27
function pe27(lim::Int)
crt = 40; p = 0
for b in primes(lim), a = -b:1000
i = 1
while isprime(i*i + a*i + b)
i += 1
end
i > crt && (crt = i; p = a*b)
end
println(p)
end
@time pe27(1000)
pe30
function PE30(n::Int)
total = 0
for i =2:n*9^n
s = 0; num = i
while num != 0
s += (num % 10)^ n
num = div(num, 10)
end
s == i && (total += s)
end
println(total)
end
@time PE30(5)
pe31
coins = [1, 2, 5, 10, 20, 50, 100, 200]
function PE31(lim::Int, i::Int)
total = 0
lim == 0 && return 1
i == 1 && return 1
for _ = 0:div(lim, coins[i])
total += pe31(lim, i-1)
lim -= coins[i]
end
total
end
@time PE31(200, 8)
pe32
function PE32()
demo = Set{Char}("123456789")
res = Set{Int}()
for a = 1:99, b = 12:9999
c = a * b
a < 10 && b < 1000 && continue
b < 100 && c < 10000 && continue
b > 100 && c > 10000 && continue
strc = string(c)
('0' in strc) && continue
d = Set{Char}()
union!(d, string(a)); union!(d, string(b)); union!(d, strc)
d == demo && union!(res, c)
end
sum(res)
end
@time PE32()
pe34
function PE34(lim::Int)
total = 0
for i =10:lim
s = 0
num = i
while num != 0
s += factorial(num % 10)
num = div(num, 10)
end
s == i && (total += s)
end
println(total)
end
@time PE34(2540160)
pe60
i4, i5, i6, i7, i8 = sqrt(s), (1+sqrt(1+24s))/6, (1+sqrt(1+8s))/4, (3+sqrt(9+40s))/10, (1+sqrt(1+3s))/3
pe62
function PE62()
count = Dict{Array{Char, 1}, Int}()
num = Dict{Array{Char, 1}, Array{Int, 1}}()
i = 1
t = sort(collect(string(i^3)))
while get(count, t, 0) != 4
idx = get(count, t, 0) + 1
count[t] = idx
num[t] = get(num, t, Array(Int, 4))
num[t][idx] = i^3
i += 1
t = sort(collect(string(i^3)))
end
println(num[t])
end
@time PE61()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment