Guest User

Untitled

a guest
Nov 2nd, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.37 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. def count_factors(n)
  4.   count = 0
  5.   c = 2
  6.   while n > 1
  7.     if n % c == 0
  8.       n /= c
  9.       count += 1
  10.     else
  11.       c += 1
  12.     end
  13.   end
  14.   count
  15. end
  16.  
  17. def prime_order(arr)
  18.   arr.sort_by! do |a,b|
  19.     fa = count_factors(a)
  20.     fb = count_factors(b)
  21.     fa == fb ? a <=> b : fa <=> fb
  22.   end
  23. end
  24.  
  25. arr = [11,8,27,4]
  26. p prime_order(arr)
  27.  
  28.  
Advertisement
Add Comment
Please, Sign In to add comment