Pyhton data's structural size and loop efficiency

In summary: Congratulations, you're done!In summary, the time it takes to factorize a composite number using this algorithm can take significantly longer than if the number was taken as input as an integer. Alternatives for drastic time reduction are needed.
  • #1
Kartik.
55
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
  • #2
I lost you at "composite".

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

how about wrapping some 'C' or 'Fortran' code?
 
  • #5
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)
 
  • #6
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.
 

Related to Pyhton data's structural size and loop efficiency

1. What is the difference between data's structural size and loop efficiency?

Data's structural size refers to the amount of space it takes up in memory, while loop efficiency refers to how quickly a loop can run through a set of data. They are two separate concepts that are both important in optimizing code performance.

2. How can I determine the structural size of my Python data?

You can use the sys.getsizeof() function to determine the size of an object in memory. This function returns the size in bytes, so you may need to convert to a larger unit for readability.

3. What factors impact the efficiency of a loop in Python?

The efficiency of a loop can be impacted by the size of the data being looped through, the complexity of the loop's logic, and the hardware and resources available on the computer running the code.

4. How can I improve the efficiency of my loops in Python?

There are several ways to improve the efficiency of loops in Python, such as using built-in functions like enumerate() or range(), avoiding unnecessary variable creation within the loop, and using efficient data structures like dictionaries or sets.

5. Is there a trade-off between data's structural size and loop efficiency in Python?

In general, there is often a trade-off between data's structural size and loop efficiency. This means that as you optimize for one, the other may be impacted. It's important to find a balance that best suits your specific code and use case.

Similar threads

Replies
1
Views
1K
Replies
13
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Linear and Abstract Algebra
Replies
2
Views
1K
  • Sci-Fi Writing and World Building
Replies
31
Views
2K
  • Programming and Computer Science
Replies
29
Views
3K
  • Other Physics Topics
Replies
9
Views
1K
  • Linear and Abstract Algebra
Replies
6
Views
4K
  • Beyond the Standard Models
Replies
28
Views
4K
Back
Top