This commit is contained in:
David Doblas Jiménez 2022-10-12 20:19:06 +02:00
parent 948dfb0ff8
commit 3cf341159b
2 changed files with 6 additions and 5 deletions

View File

@ -1,4 +1,4 @@
using Base:String using Base: String
#= #=
Created on 22 Jul 2021 Created on 22 Jul 2021
@ -15,6 +15,7 @@ function Problem13()
#= #=
Work out the first ten digits of the sum of the following one-hundred Work out the first ten digits of the sum of the following one-hundred
50-digit numbers =# 50-digit numbers =#
return string(sum(parse.(BigInt, readlines("../files/Problem13.txt"))))[1:10] return string(sum(parse.(BigInt, readlines("../files/Problem13.txt"))))[1:10]
end end

View File

@ -9,13 +9,13 @@ https://projecteuler.net/problem=14 =#
using BenchmarkTools using BenchmarkTools
function chain_length(n)# , terms) function chain_length(n)
length = 0 length = 0
while n > 1 while n > 1
n = iseven(n) ? n >> 1 : 3n + 1 n = iseven(n) ? n >> 1 : 3n + 1
length += 1 length += 1
end end
return length return length
end end
function Problem14() function Problem14()
@ -42,13 +42,13 @@ function Problem14()
ans = 0 ans = 0
limit = 1_000_000 limit = 1_000_000
score = 0 score = 0
for i in 1:2:limit # no need to check even numbers for i = 1:2:limit # no need to check even numbers
longest = chain_length(i) longest = chain_length(i)
if longest > score if longest > score
score = longest score = longest
ans = i ans = i
end end
end end
return ans return ans
end end