vladislavkopilov

kata solution

Mar 1st, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.02 KB | None | 0 0
  1. require 'prime'
  2. def factors_of(number)
  3.   primes, powers = number.prime_division.transpose
  4.   exponents = powers.map{|i| (0..i).to_a}
  5.   divisors = exponents.shift.product(*exponents).map do |powers|
  6.     primes.zip(powers).map{|prime, power| prime ** power}.inject(:*)
  7.   end
  8.   divisors.sort.map{|div| [div, number / div]}
  9. end
  10. def sigma1(nmax)
  11.   arr = []
  12.  
  13.   if( nmax>1 )
  14.     ar0 = factors_of(nmax)
  15.     ar0.each do |x,y|
  16.       arr.push(x)
  17.     end
  18.     arr.inject(0) do |sum,x|
  19.       sum + x
  20.     end
  21.   else
  22.     0
  23.   end
  24. end
  25. def equal_sigma1(nmax)
  26.   limit = nmax
  27.   arr = {}
  28.   sum = 0
  29.   while (nmax>1)
  30.     rev_nmax = nmax.to_s.reverse
  31.  
  32.     if nmax.to_s != rev_nmax && ( nmax<=limit || rev_nmax.to_i<=limit ) && sigma1( nmax )==sigma1( rev_nmax.to_i )
  33.      
  34.       if nmax<=limit
  35.         arr[nmax] = nmax
  36.       end
  37.       if rev_nmax.to_i<=limit
  38.         arr[rev_nmax.to_i] = rev_nmax.to_i
  39.       end
  40.  
  41.     end
  42.    
  43.     nmax -= 1
  44.   end
  45.  
  46.   arr.each do |var,v|
  47.     sum += var.to_i
  48.   end
  49.   #sum = 36784
  50.   sum
  51. end
Add Comment
Please, Sign In to add comment