brewersfan1976

weak_numbers.rb

Sep 25th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.28 KB | None | 0 0
  1. def weakNumbers(n)
  2.     max_weakness = 0
  3.     x = 1
  4.     number_of_divisors = 0
  5.     d = Array.new
  6.     weakness = Array.new
  7.     output = Array.new
  8.     z = 0
  9.    
  10.     while (x <= n)
  11.           y = 1
  12.        
  13.           # find the number of divisors for the current number
  14.          
  15.           while (y <= x)
  16.                 if x % y == 0 then
  17.                    number_of_divisors = number_of_divisors + 1
  18.                 end
  19.                    
  20.                 y = y + 1
  21.           end
  22.              
  23.           d[z] = number_of_divisors
  24.          
  25.           # find the weakness level
  26.          
  27.           y = 0
  28.              
  29.           while (y < d.length)
  30.                 if d[y] > number_of_divisors then
  31.                    max_weakness = max_weakness + 1
  32.                 end
  33.                    
  34.                 y = y + 1
  35.           end
  36.              
  37.           weakness[z] = max_weakness
  38.           max_weakness = 0
  39.           number_of_divisors = 0
  40.           x = x + 1
  41.           z = z + 1
  42.     end
  43.              
  44.     # find the highest weakness and the number of times it occurs
  45.              
  46.     weakness = weakness.sort
  47.     weakness = weakness.reverse
  48.     output[0] = weakness[0]
  49.     output[1] = weakness.count(weakness[0])
  50.              
  51.     return output
  52. end
Advertisement
Add Comment
Please, Sign In to add comment