http://forum.lazarus.freepascal.org/index.php/topic,29782.0.html
running $ ./euler_phi takes inestimable long time.
why?
sorry, i said something wrong just now.
it's $ ./euler_phi
10003
enter
Same answer. Look at other implementations. How do you know your code is taking a long time, if you dont compare it to other code?
All the answers are in the links above. The first one has two implementations. The second quotes this:
Another solution if you’re short in memory would not use Sieve of Eratosthenes. This way requires more processing.
IE:
This way requires more processing. The author is very clear on the up/down sides to his implementation. "More memory/faster" vs "less memory/slower".
Basically, if your code is taking a long time (compared to another algorithm), then, there often isn't any simple fix. You need to code another algorithm.
Its like asking, why is my bubblesort so slow? Because... bubblesorts are slow. Simple.
Most books Ive seen, teach this topic in the context of sorting algorithms. If you haven't learnt that, then you have skipped a lot of knowledge you need to understand why an algorithm is fast/slow and how to optimize algorithms.
https://en.wikipedia.org/wiki/Algorithmic_efficiencyhttp://www.cprogramming.com/tutorial/computersciencetheory/sortcomp.htmlhttp://rosettacode.org/wiki/Compare_sorting_algorithms'_performanceSo, I cant see how someone can explain it to you.
If you understand your code, then thats a great achievement. But, the fact is, if you want to implement the best algorithm for a given task, then you need to learn how to implement pre-designed algorithms.
Thats just how you do it. So, you need to learn to read other peoples code.