Euleryx Problem 17 – Number Letter Counts
My workflow:
Answer: 21124
Last Week's Favourite Solution:
Some great solutions, leveraging the idea breaking larger numbers up into their separate components but for keeping up the idea of dynamic building and creating a macro with a variable power toggle, last weeks favourite solution goes to @Hub119 . Please find their solutions on page one of last week's post or click here.
Mathematical Theory:
This weeks problem is all about spotting patterns. If you used all the tricks, this challenge would be relatively simple to solve by hand, however as we have Alteryx we took a combined approach meaning I utilised some patterns but also counted some values out in full. Ill explain the actual method I followed shortly, but here is a brief description of what we mean, when talking about patterns.
First write out all the numbers from 1-99 in text form i.e:
1 = one
2 = two
3 = three
Now write out all the numbers from 101-199
101= One hundred and one
102 = One hundred and two
103 = One hundred and three
You should notice something familiar. All the numbers from 1-99 appear again, just with the prefix of “One hundred and”.
For the numbers 101-199, the words “One hundred and” appear 99 times. So instead of writing this out every single time, lets just count the number of letters in it once ( 13 letters) and multiply that number by 99 instead.
This isn’t the only pattern that occurs either, ignoring the teens, the same pattern occurs with the numbers 1-9 and 21-29, 31-39…. The units 1-9 and written out the same each time, just with a different prefix of thirty or forty.
Like mentioned above, I have not fully utilised every pattern as I believe the benefit of recognising some of these in Alteryx would be negligible in terms of run time, however, the same theory can be applied.
Method:
1) Begin by generating a list of the numbers from 1 – 1000
2) Next separate out the digits into separate columns os we can lookup their respective names seperatly. I reversed the string first to make sure that all units are in the same column, regardles of the total number of digits.
3) Because the “teens” are awquard, we need to consider them seperatly. (All other numbers end in a written value of one, two …. Nine, except for the teens). To handle the teens, make sure than any values of “1” in the tens column (RowCount2), is removed, and instead placed back in the units column (Row Count 1).
4) Now we need to lookup all the digits to convert them into their text values. The “Units” lookup can be applied to the hundreds and thousands collumn to, as we will deal with the words, “hundred”, “and”, “thousand” seperatly. (Remember the values in the initial RowCount column are reversed).
5) Pivot the “Units”, “Tens”, “Hundreds” and “Thousands” columns down so that all the numetic text is in the one column.
6) Count the number of charactors for each of the numbers written in the “Value” column, then sum these counts.
7) Next, we need to consider the number of times the words “Hundred” or “Thousand” appear. This will equal the number of values found within the respective columns.
8) Multiply these counts by the character count from the corresponding words.
9) Then we need to figure out how many times the word “and” appears. The word and will be used every time there is a value in the “hundreds” column, and in either the tens or units column.
10) Similarly to before, we need to count the occourances and multiple this by the number of charactors in the word “and”.
11) Finally, we can sum these three totals to get to our answer
12) Submit your answer to the Project Euler Website!
Summary:
By spotting repetition within our sequence of numbers we were able to minimise the calculations required by counting the characters of certain strings once, then multiplying the values by the number of times they occur.
Want to find out more, follow this link to our introduction post - Euleryx: Let The Games Begin.
Mine is quite lengthy but get what I need.
I created an input with 29 representative numbers, and then processed the rest using the Generate Row tool and the Join tool.
Minimal tools but LOTS of IF statement coding on this one...
Unlike last week, I know my solution is not nice (in fact it's ugly) - I think I just didn't have a good enough understanding of the logic behind it.
