How to resolve the algorithm Happy numbers step by step in the Ruby programming language
How to resolve the algorithm Happy numbers step by step in the Ruby programming language
Table of Contents
Problem Statement
From Wikipedia, the free encyclopedia:
Find and print the first 8 happy numbers. Display an example of your output here on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Happy numbers step by step in the Ruby programming language
The code you provided is a Ruby program that finds and prints happy numbers. A happy number is a number that eventually reaches 1 after repeatedly replacing the number with the sum of the squares of its digits. For example, 19 is a happy number because 19^2 = 361, 361^2 = 1369, 1369^2 = 2041, 2041^2 = 4225, 4225^2 = 1881, 1881^2 = 3241, 3241^2 = 10201, 10201^2 = 11441, 11441^2 = 12976, 12976^2 = 16384, 16384^2 = 26725, 26725^2 = 71225, 71225^2 = 51451, 51451^2 = 26725, 26725^2 = 71225, 71225^2 = 51451, 51451^2 = 26725, 26725^2 = 71225, 71225^2 = 51451, 51451^2 = 26725, 26725^2 = 71225, 71225^2 = 51451, 51451^2 = 26725, 26725^2 = 71225, 71225^2 = 51451, 51451^2 = 26725, 26725^2 = 71225, 71225^2 = 51451, and 51451^2 = 26725. Because the number eventually reaches 1, it is a happy number.
The code uses a Set data structure to track numbers that have already been seen, and a Set data structure to track numbers that have been determined to be happy. The happy?
method takes a number as a parameter and returns true
if the number is happy, and false
otherwise. The method uses the seen_numbers
Set to track numbers that have already been seen, and the happy_numbers
Set to track numbers that have been determined to be happy. The method first checks if the number is 1, in which case it returns true
because 1 is a happy number. If the number is not 1, the method checks if it has already been seen. If it has, the method returns true
if the number is in the happy_numbers
Set, and false
otherwise. If the number has not been seen, the method adds it to the seen_numbers
Set and calculates the sum of the squares of its digits. The method then calls itself recursively with the sum of the squares of the digits as the argument. If the recursive call returns true
, the method adds the number to the happy_numbers
Set and returns true
. Otherwise, the method returns false
.
The print_happy
method prints the first 8 happy numbers. The method uses a loop to iterate over the numbers starting at 1. For each number, the method calls the happy?
method to check if it is happy. If the number is happy, the method adds it to an array. The method then prints the array of happy numbers.
The second code block is another way to find happy numbers. It uses a memoization technique to store the results of previous calculations. This makes the algorithm faster for large numbers.
The third code block is a third way to find happy numbers. It uses a loop to iterate over the numbers starting at 1. For each number, the method calls the happy?
method to check if it is happy. If the number is happy, the method prints it.
Source code in the ruby programming language
require 'set' # Set: Fast array lookup / Simple existence hash
@seen_numbers = Set.new
@happy_numbers = Set.new
def happy?(n)
return true if n == 1 # Base case
return @happy_numbers.include?(n) if @seen_numbers.include?(n) # Use performance cache, and stop unhappy cycles
@seen_numbers << n
digit_squared_sum = n.digits.sum{|n| n*n}
if happy?(digit_squared_sum)
@happy_numbers << n
true # Return true
else
false # Return false
end
end
def print_happy
happy_numbers = []
1.step do |i|
break if happy_numbers.length >= 8
happy_numbers << i if happy?(i)
end
p happy_numbers
end
print_happy
[1, 7, 10, 13, 19, 23, 28, 31]
@memo = [0,1]
def happy(n)
sum = n.digits.sum{|n| n*n}
return @memo[sum] if @memo[sum]==0 or @memo[sum]==1
@memo[sum] = 0 # for the cycle check
@memo[sum] = happy(sum) # return 1:Happy number, 0:other
end
i = count = 0
while count < 8
i += 1
puts i or count+=1 if happy(i)==1
end
puts
for i in 99999999999900..99999999999999
puts i if happy(i)==1
end
def happy?(n)
past = []
until n == 1
n = n.digits.sum { |d| d * d }
return false if past.include? n
past << n
end
true
end
i = count = 0
until count == 8; puts i or count += 1 if happy?(i += 1) end
puts
(99999999999900..99999999999999).each { |i| puts i if happy?(i) }
You may also check:How to resolve the algorithm Averages/Median step by step in the Craft Basic programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the Racket programming language
You may also check:How to resolve the algorithm Parameterized SQL statement step by step in the C++ programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Octave programming language
You may also check:How to resolve the algorithm Matrix digital rain step by step in the Wren programming language