Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.41 KB | None | 0 0
  1. # well, you could have an absolute maximum of 5 3s
  2. # 16 1s
  3. # so i'd start off with 5 3s and a 1
  4. # remove 1 3 and add 3 1s
  5. # loop until 16 1's
  6. # for each result, find the total number of possible combinations
  7. # so say the result is [3,3,3,1,1,1,1,1,1,1]
  8. # you'd have to find the total possible number of results without using
  9. # any more or any less than 3 3s
  10. # and 7 1s
  11. # and without repeating a combination
  12. # Therefore:
  13.  
  14. $max_3s = 5;
  15. $max_1s = 16;
  16.  
  17. # Define $sum as the sum of the current loop's values.
  18. # If $sum > 16 at any point, or < 16 when loop ends, discard the value.
  19. $sum = 0;
  20.  
  21. # $sum_3s is the number of 3s in the current loop.
  22. # We don't need 1s; if there are more than 16 1s then
  23. # the limit of sixteen has already been reached.
  24. $sum_3s = 0;
  25.  
  26. # Define $valid as an array containing all valid strings.
  27. # Will use valid.length to get the total number.
  28. $valid = [];
  29.  
  30. $startstr = "133333";
  31. $str = $startstr;
  32.  
  33. # Function to return the next string.
  34. def nextStr(str)
  35.   orig = str;
  36.   str = str.sub('3', '1');
  37.   return (str==orig ? false : str);
  38. end
  39.  
  40. while true do
  41.   $str.each_byte { |ch|
  42.     $sum += ch.to_i;
  43.     if (ch.to_i == 3) then $sum_3s += 1; end
  44.     if ($sum > 16) or ($sum_3s > $max_3s) then
  45.       $sum=0;
  46.       break;
  47.     end
  48.   }
  49.   if not ($valid.index($str)) then
  50.     $valid.push($str);
  51.   end
  52.   $str = nextStr($str);
  53.   break if not $str;
  54. end
  55. print $valid.length, " values.\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement