ruby - All factors of a given number -


For example, I have 4800 and I want to see all the factors of this number.

  # num = Number which you want def factors_of (num) (1..num) factors .collect {| N | [N, num / n] if ((num / n) * n) == num} .compact end  

divisors_of (4800) => [[1, 4800] , [3, 1600], [4, 1200], [5, 960], [6, 800], [8, 600], [10, 480], [12, 400], [ 15, 320] [16, 300], [20, 240], [24, 200], [25, 1 9 2], [30, 160], [32, 150], [40, 120], [48] , [80, 60], [96, 50], [64, 80], [64, 80], [64, 75], [100, 48], [120, 40], [680] 100, 48], [3, 3, 16], [320, 15], [400, 12], [150, 32], [160, 30], [192, 25], [200, 24] [4, 8, 8, 8, 8], [960, 5], [1200, 4], [1600, 3], [2400, 2], [4800] 1]]

How do you do it in Ruby or any language?

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -