Created
September 18, 2012 15:47
-
-
Save aycabta/3743849 to your computer and use it in GitHub Desktop.
Calculate 2^n that is computable even where n is larger than ULONG_LONG_MAX.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
factor = 65536 | |
cy_pos = 0 | |
cy_interval_list = Array.new | |
next_cy_interval_list = Array.new | |
cy_interval_count = 0 | |
digit = 1 | |
num_of_digit = 1 | |
is_cy = false | |
num_str = String.new | |
begin | |
factor_this_digit = factor - cy_pos | |
puts "rest: #{factor_this_digit}\n" | |
cy_interval = cy_interval_list.shift | |
is_cy = false | |
factor_this_digit.times do |num| | |
num_of_digit *= 2 | |
if not cy_interval.nil? | |
cy_interval -= 1 | |
if cy_interval == 0 | |
num_of_digit += 1 | |
cy_interval = cy_interval_list.shift | |
end | |
end | |
cy_interval_count += 1 | |
if num_of_digit >= 10 | |
num_of_digit %= 10 | |
if not is_cy | |
cy_pos += num + 1 | |
is_cy = true | |
else | |
next_cy_interval_list << cy_interval_count | |
end | |
cy_interval_count = 0 | |
end | |
end | |
num_str = num_of_digit.to_s + num_str | |
# swap | |
temp = cy_interval_list | |
cy_interval_list = next_cy_interval_list | |
next_cy_interval_list = temp | |
next_cy_interval_list.clear | |
num_of_digit = 1 | |
digit += 1 | |
end while is_cy | |
puts "#{num_str}\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment