Why is the divide and conquer method of computing factorials so fast for large ints? [closed]
2024/11/17 13:33:04
I recently decided to look at factorial algorithms for large integers, and this "divide and conquer" algorithm is faster than a simple iterative approach and the prime-factoring approach:
def multiply_range(n, m):print n, mif n == m:return nif m < n:return 1else:return multiply_range(n, (n+m)/2) * multiply_range((n+m)/2+1, m)def factorial(n):return multiply_range(1, n)
I understand why the algorithm works, it just breaks the multiplication into smaller parts recursively. What I don't understand is why this method is faster.
Answer
Contrary to @NPE's answer, your method is faster, only for very large numbers.
For me, I began to see the divide and conquer method become faster for inputs ~10^4. At 10^6 and above there is no comparison a traditional loop fails miserably.
I'm no expert on hardware multipliers and I hope someone can expand on this, but my understanding is that multiplication is done digit for digit same way we are taught in grade school.
A traditional factorial loop will start with small numbers and the result keeps growing. In the end you are muliplying a ginormous number with a comparatively small number, an expensive calculation due to the mismatch in digits.
the second is slower because the result grows fast, leading to more expensive calculations sooner.
Your method is faster than either of these by orders of magnitude for large numbers because it divides the factorial into similarly sized parts. The sub-results have similar numbers of digits and multiply faster.
How do I calculate the speed, distance and direction (degrees) from 2 GPS coordinates in Python? Each point has lat, long, time.I found the Haversine distance calculation in this post:Calculate dista…
Im new to gunicorn and trying to deploy a django website on an ubuntu.
I have used: pip3 install gunicorn
sudo apt-get install gunicornbut when I want to fill this file:sudo nano /etc/systemd/system/g…
If I have two functions doingasync with mylock.acquire():....Once the lock is released, is it guaranteed that the first to await will win, or is the order selected differently? (e.g. randomly, arbitra…
Im writing a crossplatform python script on windows using Eclipse with the Pydev plugin. The script makes use of the os.symlink() and os.readlink() methods if the current platform isnt NT.
Since the os…
Im using the following procedure to calculate hexagonal polygon coordinates of a given radius for a square grid of a given extent (lower left upper right):def calc_polygons(startx, starty, endx, endy,…
I could be missing something fundamental, but consider this interpreter session1:>>> -0.0 is 0.0
False
>>> 0.0 is 0.0
True
>>> -0.0 # The sign is even retained in the output…
In scrapy, I am getting the error exceptions.AttributeError: unicode object has no attribute dont_filter. After searching around, I found this answer (which made sense as it was the only bit of code I …
Is there a established way that i validate an object in the dispatch without making an extra database call when self.get_object() is called later in get/post?Here is what i have so far (slightly alter…
Im studying algorithms and decided to port the Java Programs from the textbook to Python, since I dislike the Java overhead, especially for small programs, and as an exercise.The algorithm itself is ve…
My Python Interpreter (v2.6.5) raises the above error in the following codepart:fd = open("some_filename", "r")
fd.seek(-2, os.SEEK_END) #same happens if you exchange the second arg…