Pyhton data's structural size and loop efficiency

AI Thread Summary
The discussion centers on the challenges of efficiently finding the largest prime factor of a composite number, particularly when the input exceeds a certain limit, leading to significantly increased loop times. The standard method involves looping through natural numbers to identify factors and checking their primality, which can become time-consuming. Participants suggest alternatives for improving efficiency, such as implementing the Sieve of Eratosthenes to generate prime numbers up to the square root of the input. The proposed approach includes checking for prime factors starting from the smallest prime, dividing the input by any found factors to reduce the number for subsequent tests, and continuing this process until reaching the largest prime smaller than the square root. If no factors are found in this range, the input is concluded to be prime.
Kartik.
Messages
55
Reaction score
1
While programming to find the largest prime factor for a composite number i came across a problem that if the number taken as input exceeds a certain limit, the loop time turns out to be a very long one and some times even resembling the time taken by an infinite loop.The method used for finding the prime factors is a standard one in which we loop through natural number until the input equals the loop counter; finding its factors and then checking for it to be prime or not ;through another function by a similar method using a counter variable.? Alternatives for drastic time reduction ?
 
Technology news on Phys.org
I lost you at "composite".

time reduction of what? you going to show us some code?

how about wrapping some 'C' or 'Fortran' code?
 
gsal said:
I lost you at "composite".

time reduction of what? you going to show us some code?

how about wrapping some 'C' or 'Fortran' code?

I wrote about the time it takes to loop through natural number during program execution and wanted any time-efficient method for factorization.(They have wiki pages for that too :P)
 
I guess the simplest algorithm would be something like this:

(1) find an efficient way to generate all prime numbers up to the square root of the input. Something like Sieve. Or use a pre-calculated table if you know what the largest input number is.

(2) start checking from the smallest prime=2 up.

(3) as soon as you find a prime factor, divide the input number by this factor. Do this several times if the factor occurs in a higher power than 1. Dividing will make the test number smaller and subsequent tests faster (assuming your input is larger than standard 32-bit integers).

(4) repeat with the next largest prime, up to the largest prime smaller than the square root.

(5) if you don't find a prime factor smaller than the square root, then the input number is a prime.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top