Last active
August 29, 2023 02:56
-
-
Save james-see/5b414e249e6ed552e4ee7c75b6b0a8af to your computer and use it in GitHub Desktop.
fast way to create a 1000000 digit number
This file contains hidden or 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
import sys | |
import multiprocessing | |
import math | |
sys.set_int_max_str_digits(10000000) | |
def worker_function(dummy_arg): | |
"""Function to calculate 10**100000 and return its length.""" | |
result = 10**1000000 | |
return result | |
def count_digits(number): | |
if number == 0: | |
return 1 | |
# Use the logarithmic property to calculate the number of digits | |
digit_count = int(math.log10(abs(number))) + 1 | |
return digit_count | |
if __name__ == "__main__": | |
# Get the number of available CPU cores | |
num_cores = multiprocessing.cpu_count() | |
# Create a pool of worker processes | |
pool = multiprocessing.Pool(processes=10) | |
# Run the worker function in parallel using the pool | |
results = pool.map(worker_function, range(10)) | |
# Close the pool and wait for the worker processes to finish | |
pool.close() | |
pool.join() | |
# count digits mf | |
total = sum(results) | |
totalcounted = count_digits(total) | |
print("Total digits in the sum of 10**100000:", total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
reference: what is Sieve of Erasothenes