Advertisement
Zac_McDonald

Ones Combos - Ruby

Sep 22nd, 2019
1,832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.92 KB | None | 0 0
  1. $results = { :ignore => [], 1 => [], 2 => [], 4 => [], 8 => [] }
  2.  
  3. def sum_array (numbers)
  4.     n = 0
  5.     for i in 0...numbers.length
  6.         n += numbers[i]
  7.     end
  8.     return n
  9. end
  10.  
  11. def dec_to_array (n)
  12.     array = []
  13.     numbers = [1,2,4,8,6,2,4].reverse()
  14.     for i in 0..6
  15.         a = 2.pow(i)
  16.         if (n & a == a)
  17.             array.push(numbers[i])
  18.         end
  19.     end
  20.     return array
  21. end
  22.  
  23. # Calculate included and excluded values
  24. for i in 0...128
  25.     array = dec_to_array(i)
  26.     sum = sum_array(array)
  27.     ones = sum % 10
  28.     if (sum > 99)
  29.         $results[:ignore].push(i)
  30.     else
  31.         # Fill out bit-results
  32.         for j in 0..3
  33.             a = 2.pow(j)
  34.             if (ones & a == a)
  35.                 $results[a].push(i)
  36.             end
  37.         end
  38.     end
  39. end
  40.  
  41. File.open("./ones_combos.txt", 'w') { |file|
  42.     file.puts "128 total combinations"
  43.     for i in 0...$results.keys.length
  44.         result = $results[$results.keys[i]]
  45.         file.puts "------------|| #{$results.keys[i]} (#{result.length} combos) ||------------"
  46.         for j in 0...result.length
  47.             dec = result[j]
  48.             file.puts "#{dec_to_array(dec).to_s} = #{dec}"
  49.         end
  50.     end
  51.     file.puts "\n"
  52.     for i in 0...$results.keys.length
  53.         result = $results[$results.keys[i]]
  54.         file.puts "------------|| #{$results.keys[i]} (#{result.length} combos) ||------------"
  55.         for j in 0...result.length
  56.             file.print "#{result[j]}"
  57.             if (j < result.length - 1)
  58.                 file.print(",")
  59.             end
  60.         end
  61.         file.print("\n")
  62.     end
  63. }
  64.  
  65. # MAPPING RESULTS
  66.  
  67. # 1-bit
  68. # F = 1
  69.  
  70. # 2-bit
  71. # F = 2'4'8'62'4' + 24'862'4' + 2'4'8624' + 248'6'24 + 24'8'6'2' + 2'4'8'6'2 + 248'2'4' + 2'486'2' + 2'48'24' + 2'86'2'4 + 24'86'2 + 28'62'4 + 2'482'4 + 2'8'624 + 24824' + 28624                                          
  72.  
  73. # 4-bit
  74. # F = 24'8'6'24' + 2'486'2'4 + 248'624' + 24'8'624 + 2'48'6'4' + 2'4'62'4' + 2'4'8'6'4 + 246'2'4' + 24'6'2'4 + 4'862'4' + 2'4'864' + 486'24' + 2'48'64 + 4'86'24 + 2462'4 + 48624
  75.  
  76. # 8-bit
  77. # F = 2'4'86'2'4' + 24'8'62'4' + 2'48'6'2'4 + 2'4'8'624' + 248'6'24' + 24'8'6'24 + 2'4862'4' + 2'4'862'4 + 2486'2'4 + 24'8624' + 2'486'24 + 248'624
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement