![]() All other access is a simple Hash lookup and generally quite fast since Ruby's Hash is written in C. By creating a Hash and providing a block that can calculate the values from the keys, we ensure that Ruby will only run the code the first time it is needed. This is the standard Hash memoization pattern Ruby Quiz regulars are probably pretty familiar with by now. freezeÄigits = num.to_s.split( "").sort.map Here's the code I used to generate the above list, which just loops over the sum of the squares until a repeat is found: ![]() That's really just a different way to find the repeat pattern mentioned in the quiz: I posted a link to some additional information early in the quiz discussion that expanded on the definition provided by the quiz:Īccording to that document, you can tell that a number is unhappy if the sum of the squares of the digits ever reaches 0, 4, 16, 20, 37, 42, 58, 89, or 145. A happy bases is a base where all numbers are happy. From there you can extend the program so that it finds happy bases (other than 2 and 4). If you find all these examples trivial, write you program so that it will find happy numbers in other bases such as base 2 or 16. 7 found four other numbers (49, 97, 130, and 10) making it a rank 4 in happiness. ![]() I define the happiest number as the smallest number that finds the most other happy numbers with it, i.e. Now that you have this program, what is the largest happy number you can find? What is the happiest number between 1 and 1,000,000. If a number is not happy than it is obviously unhappy. A happy number is found using the following process: Take the sum of the squares of its digits, and continue iterating this process until it yields 1, or produces an infinite loop. Write a program that tells whether a given integer is happy.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |