Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # well, you could have an absolute maximum of 5 3s
- # 16 1s
- # so i'd start off with 5 3s and a 1
- # remove 1 3 and add 3 1s
- # loop until 16 1's
- # for each result, find the total number of possible combinations
- # so say the result is [3,3,3,1,1,1,1,1,1,1]
- # you'd have to find the total possible number of results without using
- # any more or any less than 3 3s
- # and 7 1s
- # and without repeating a combination
- # Therefore:
- $max_3s = 5;
- $max_1s = 16;
- # Define $sum as the sum of the current loop's values.
- # If $sum > 16 at any point, or < 16 when loop ends, discard the value.
- $sum = 0;
- # $sum_3s is the number of 3s in the current loop.
- # We don't need 1s; if there are more than 16 1s then
- # the limit of sixteen has already been reached.
- $sum_3s = 0;
- # Define $valid as an array containing all valid strings.
- # Will use valid.length to get the total number.
- $valid = [];
- $startstr = "133333";
- $str = $startstr;
- # Function to return the next string.
- def nextStr(str)
- orig = str;
- str = str.sub('3', '1');
- return (str==orig ? false : str);
- end
- while true do
- $str.each_byte { |ch|
- $sum += ch.to_i;
- if (ch.to_i == 3) then $sum_3s += 1; end
- if ($sum > 16) or ($sum_3s > $max_3s) then
- $sum=0;
- break;
- end
- }
- if not ($valid.index($str)) then
- $valid.push($str);
- end
- $str = nextStr($str);
- break if not $str;
- end
- print $valid.length, " values.\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement