Calculate the square root of 2 in millions of digits with python

Create a program that calculates the parallel roots of 2 in millions of digits with python. The calculation method uses the following Newton iterative method for the reciprocal. Iterative: x = x + x * (1-x * x / 2) / 2 The feature of this method is that there is no multi-digit division. The n-digit calculation part in python is below. def sqrt2(n):  bit, dec = 40, 12  d12 = 100001000010000  x = int( math.sqrt(2)(1 << bit) )  while dec <= n:   dec = dec << 1   d2 = 1 << (2bit)   x0 = (xx) >> 1   x1 = (d2 - x0) >> 1   x2 = (xx1) >> bit   x = (x << bit) + x2 + 1   bit = 2bit   d12 = d12d12   x = (x*d12) >> bit  dec_o = (n // 100)*100  return x

format (x) is required to convert the calculation result of x = sqrt2 (n) to a decimal number. The entire python is posted in the python program section at https://ecc-256.com. Download sqrt2 and change sqrt2.py and the first import to import. Type python sqrt2.py at the command prompt. Next, enter the number of output digits. 1000000 for 1 million digits

The calculation time of 3 million, 6 million, 12 million of windows 10 personal computer (4 Ghz) is as follows. x=sqrt(n) : 6.7, 19.9, 59.7 (s) format(x) : 136, 545, 2180 (s)

Decimal conversion for output takes much longer than the calculation of sqrt (2). sqrt (2) takes twice as many digits and three times as long, and decimal conversion takes four times as long. The calculation multiplication is the Karatsuba method, and the conversion multiplication is due to the definition formula.

Even in python, it can be speeded up by scaling with about 1000 decimal digits (value is binary int) and applying high-speed remainder conversion (FMT). The goal is to calculate 100 million digits and convert it to decimal characters within 3 minutes on a 4Ghz personal computer (end of February).

Recommended Posts

Calculate the square root of 2 in millions of digits with python
Calculate the total number of combinations with python
[Python] Calculate the number of digits required when filling in 0s [Note]
How to get the number of digits in Python
Calculate the regression coefficient of simple regression analysis with python
Try scraping the data of COVID-19 in Tokyo with Python
[Homology] Count the number of holes in data with Python
Calculate the previous month in Python
Output the contents of ~ .xlsx in the folder to HTML with Python
Visualize the frequency of word occurrences in sentences with Word Cloud. [Python]
Tips: [Python] Calculate the average value of the specified area with bedgraph
Check the behavior of destructor in Python
Check the existence of the file with python
Display Python 3 in the browser with MAMP
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
Memo of the program to get the date in two digits with javascript, Ruby, Python, shell script
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Calculate the probability of being a squid coin with Bayes' theorem [python]
Receive a list of the results of parallel processing in Python with starmap
I tried hundreds of millions of SQLite with python
Output the number of CPU cores in Python
[Python] Get the files in a folder with Python
[Python] Sort the list of pathlib.Path in natural sort
Prepare the execution environment of Python3 with Docker
[Note] Export the html of the site with python.
Match the distribution of each group in Python
View the result of geometry processing in Python
[Automation] Extract the table in PDF with Python
Make a copy of the list in Python
Check the date of the flag duty with Python
Find the divisor of the value entered in python
Load the network modeled with Rhinoceros in Python ②
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
Convert the character code of the file with Python3
One-liner that outputs 10000 digits of pi with Python
[Python] Determine the type of iris with SVM
Load the network modeled with Rhinoceros in Python ①
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Compare the sum of each element in two lists with the specified value in Python
[Numpy, scipy] How to calculate the square root of a semi-fixed definite matrix
How to get a list of files in the same directory with python
the zen of Python
Display n digits after the decimal point in python
Experience the good calculation efficiency of vectorization in Python
Extract the table of image files with OneDrive & Python
Learn Nim with Python (from the beginning of the year).
[Python] Get the numbers in the graph image with OCR
Destroy the intermediate expression of the sweep method with Python
[python] Get the list of classes defined in the module
Visualize the range of interpolation and extrapolation with python
Crawl the URL contained in the twitter tweet with python
The story of FileNotFound in Python open () mode ='w'
Convert the image in .zip to PDF with Python
One liner that outputs 1000000 digits of pi in Python