brewersfan1976

square_digits_sequence.rb

Jan 1st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.71 KB | None | 0 0
  1. def squareDigitsSequence(a0)
  2.     array = Array.new
  3.     array[0] = a0
  4.     y = 0
  5.     z = 0
  6.     string = a0.to_s
  7.     result = 0
  8.     counter = 1
  9.      
  10.     while (1)
  11.           while (y < string.length)
  12.                 result = result + (string[y].to_i ** 2)
  13.                 y = y + 1
  14.           end
  15.          
  16.           counter = counter + 1
  17.          
  18.           while (z < array.length)
  19.                 if array[z] == result then
  20.                    return counter
  21.                 end
  22.                      
  23.                 z = z + 1
  24.           end
  25.          
  26.           array.push(result)              
  27.           string = result.to_s
  28.           y = 0
  29.           z = 0
  30.           result = 0
  31.     end
  32. end
Advertisement
Add Comment
Please, Sign In to add comment