Last active
November 5, 2019 16:33
-
-
Save sushant12/a7c3175b3eb60c3c793656103f2551af to your computer and use it in GitHub Desktop.
find curious numbers by string manipulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CuriousNumbers | |
attr_reader :digit, :largest_num | |
BASE_CURIOUS_NUMS = [0, 1, 5, 6].freeze | |
def initialize(digit) | |
@digit = digit.abs | |
@largest_num = ('9'*@digit).to_i | |
end | |
def execute | |
return [0] if digit == 0 | |
(BASE_CURIOUS_NUMS + squared_numbers(25) + squared_numbers(36)).sort | |
end | |
private | |
def squared_numbers(num) | |
squared_number = num | |
arr = [] | |
until squared_number > largest_num | |
arr.push(squared_number) | |
squared_number = squared_number ** 2 | |
end | |
arr | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment