Fix error in calculation
This commit is contained in:
parent
90bfb60fff
commit
c99257d48f
@ -1,4 +1,4 @@
|
|||||||
#=
|
#=
|
||||||
Created on 28 Jul 2021
|
Created on 28 Jul 2021
|
||||||
|
|
||||||
@author: David Doblas Jiménez
|
@author: David Doblas Jiménez
|
||||||
@ -9,38 +9,7 @@ https://projecteuler.net/problem=17 =#
|
|||||||
|
|
||||||
using BenchmarkTools
|
using BenchmarkTools
|
||||||
|
|
||||||
function num2letters(num, dic)
|
function num2letters(num)
|
||||||
if num <= 20
|
|
||||||
return length(dic[num])
|
|
||||||
elseif num < 100
|
|
||||||
tens, units = divrem(num, 10)
|
|
||||||
return length(dic[tens * 10]) + num2letters(units, dic)
|
|
||||||
elseif num < 1000
|
|
||||||
hundreds, rest = divrem(num, 100)
|
|
||||||
if rest != 0
|
|
||||||
return num2letters(hundreds, dic) + length(dic[100])
|
|
||||||
+ length("and") + num2letters(rest, dic)
|
|
||||||
else
|
|
||||||
return num2letters(hundreds, dic) + length(dic[100])
|
|
||||||
end
|
|
||||||
else
|
|
||||||
thousands, rest = divrem(num, 1000)
|
|
||||||
return num2letters(thousands, dic) + length(dic[1000])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function Problem17()
|
|
||||||
#=
|
|
||||||
If the numbers 1 to 5 are written out in words: one, two, three, four,
|
|
||||||
five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
|
|
||||||
|
|
||||||
If all the numbers from 1 to 1000 (one thousand) inclusive were written
|
|
||||||
out in words, how many letters would be used?
|
|
||||||
|
|
||||||
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and
|
|
||||||
forty-two) contains 23 letters and 115 (one hundred and fifteen) contains
|
|
||||||
20 letters. The use of "and" when writing out numbers is in compliance
|
|
||||||
with British usage. =#
|
|
||||||
nums = Dict(
|
nums = Dict(
|
||||||
0 => "", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five",
|
0 => "", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five",
|
||||||
6 => "six", 7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten",
|
6 => "six", 7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten",
|
||||||
@ -51,10 +20,43 @@ function Problem17()
|
|||||||
90 => "ninety", 100 => "hundred", 1000 => "thousand"
|
90 => "ninety", 100 => "hundred", 1000 => "thousand"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if num <= 20
|
||||||
|
return length(nums[num])
|
||||||
|
elseif num < 100
|
||||||
|
tens, units = divrem(num, 10)
|
||||||
|
return (length(nums[Int(tens) * 10]) + num2letters(units))
|
||||||
|
elseif num < 1000
|
||||||
|
hundreds, rest = divrem(num, 100)
|
||||||
|
if rest != 0
|
||||||
|
return (num2letters(hundreds) + length(nums[100])
|
||||||
|
+ length("and") + num2letters(rest))
|
||||||
|
else
|
||||||
|
return (num2letters(hundreds) + length(nums[100]))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
thousands, rest = divrem(num, 1000)
|
||||||
|
return (num2letters(thousands) + length(nums[1000]))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Problem17()
|
||||||
|
#=
|
||||||
|
If the numbers 1 to 5 are written out in words: one, two, three, four,
|
||||||
|
five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
|
||||||
|
|
||||||
|
If all the numbers from 1 to 1000 (one thousand) inclusive were written
|
||||||
|
out in words, how many letters would be used?
|
||||||
|
|
||||||
|
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and
|
||||||
|
forty-two) contains 23 letters and 115 (one hundred and fifteen) contains
|
||||||
|
20 letters. The use of "and" when writing out numbers is in compliance
|
||||||
|
with British usage. =#
|
||||||
|
|
||||||
n = 1000
|
n = 1000
|
||||||
letters = 0
|
letters = 0
|
||||||
for num in 1:n
|
for num in 1:n
|
||||||
letters += num2letters(num, nums)
|
letters += num2letters(num)
|
||||||
end
|
end
|
||||||
return letters
|
return letters
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user