Extension for Module 8:
Extension 8.1: Sad Cycles (5 points)
- Authors
-
Fede Rozenberg
Overview
So it turns out that numbers can feel things. We define a happy or sad number through this process:
-
Split up the number into its digits.
-
Square the digits and sum them.
-
Repeat with the new sum until you reach a set of numbers that repeat or the number 1.
-
If your process results in a repeating 1, your initial number was happy! Otherwise, it has fallen into a cycle of depression, characterized by the repeating numbers
[4, 16, 37, 58, 89, 145, 42, 20, 4...]How sad.
For example, starting with the number 15, our process yields:
- 12 + 52 = 26
- 22 + 62 = 40
- 42 + 02 = 16
- 12 + 62 = 37
- 32 + 72 = 58
Here you can see our number has fallen into the beginning stages of depression…
In this extension you will be designing a program that uses sets to find the sad cycle of a number for an arbitrary power.
Procedure
In the sadcycle package of your extensions folder, create a SadCycler class.
-
Create a method
Set<Long> findCycle(int base, long n)that takes in a numbernand either returns aSet<Long>with thebase-sad cycle fornor with 1 as its only element.A
longis simply twice as long as anint. I recommend using it here because some of the numbers found in higherbase-sad cycles can be quite large. -
Feel free to create any other methods you might need.
I highly recommend partitioning the splitting of digits, raising to the power of
base, and summing into one method. The trick here is figuring out how to treat every digit in a number as its own entity. -
Test your program on inputs you find documented here. Show these results to a TA and convince him or her they are right for credit on this assignment.
Submitting your work
To submit your work, contact the instructor or a TA. They will walk you through the submission process. Be prepared to show them the work that you have done and answer their questions about it!