Skip to content

Delphi Fusion

Narrow screen resolution Wide screen resolution Increase font size Decrease font size Default font size    Default color brown color green color red color blue color
  • Regular Delphi programming articles and tutorials
  • Free members forum supporting everything Delphi
  • Support for all programming abilities
You are here: Home arrow Articles arrow Delphi arrow Algorithm Efficiency - Prime Numbers
Skip to content

User Menu

Login
Register
Logout

Latest Threads

Delphi and C++Builder Roadmap
by: Jon!
@: 03/09/08 11:53 am
Delphi 7 Enterprise!
by: uuf6429
@: 17/06/08 01:33 pm
Tracing file/registry activity
by: uuf6429
@: 17/06/08 01:25 pm

Delphi Fusion

Links
Sponsors
Amazon

Donate

Enter Amount:


Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285
Algorithm Efficiency - Prime Numbers Print E-mail
Written by Blueaura   

The purpose of this tutorial is to demonstrate how an algorithm is first formed as a solution to a problem, and also how we can make the algorithm more efficient in terms of speed. The problem we will be looking at is how to check which numbers are prime numbers within the range of 1 to 10,000,000.

First of all what is a prime number? A mathematician would say a prime is “a number which is only divisible by two numbers, 1 and itself”. You should note at this point that 1 is not a prime number because it is only divisible by one number, 1. So now we know what a prime is how are we going to test for one? With complex problems the simplest solutions are often the most effective, we will adopt this philosophy and code the simplest solution and then build upon this.

The simplest solution would be to increment a variable X from 1 to 10,000,000 and have a nested loop within this incrementing a variable Y from 1 to X and trying to divide X by Y. We also need an initial check in there to see if the number is less than 2, which would not be a prime. You should get something like this in Delphi code which at this stage I suggest you do not run:

Simple Solution
  1. function PrimeCycle: string;
  2. var
  3. X: Integer;
  4. begin
  5. Result := '';
  6. X := 1;
  7. while X <= 10000000 do
  8. begin
  9. if BasicIsPrime(X) then
  10. Result := Result + IntToStr(X) + ', ';
  11. Inc(X);
  12. end;
  13. end;
  14.  
  15. function BasicIsPrime(Number: Integer): Boolean;
  16. var
  17. Y: Integer;
  18. begin
  19. if Number <= 1 then
  20. Result := False
  21. else
  22. begin
  23. Y := 2;
  24. Result := True;
  25. while Y < Number do
  26. begin
  27. if Number mod Y = 0 then
  28. begin
  29. Result := False;
  30. Break;
  31. end;
  32. Inc(Y);
  33. end;
  34. end;
  35. end;
 
Note: Notice how the loops are in separate functions. This is to make the code easier to read, easier to maintain, and is more flexible because either function can now be used independently elsewhere.

Now let’s discuss the efficiency of this solution. Being the simplest solution to the problem it has a major flaw, although the ‘BasicIsPrime’ function works and produces no false positives or negatives it takes at least three hours on a fast modern PC to complete. So we need to do something about this and the best place to start is to go back to the drawing board and look at the how we are identifying a prime number.

So what we are doing is iterating through 2 until X – 1 to establish if any value of Y is a factor of X. If we look at this logically there is no need to iterate up until X – 1. If we look at X = 100, and therefore define the range as 2 to 99 then we only actually need to iterate through until X/2, i.e. 2 to 50. This is simply because if we are dividing X by 50 we are also dividing it by 100 as 100 is a multiple of 50, and so on; therefore the range 2 to X/2 actually covers all of the range 2 to X – 1.

What we have done so far is halved the range we need to check, and we can time this using the GetTickCount function:

Simple Solution - Version2
  1. function PrimeCycle(var Speed: Double): string;
  2. var
  3. X: Integer;
  4. Start,
  5. Finish: Cardinal;
  6. begin
  7. Result := '';
  8. X := 1;
  9. Start := GetTickCount;
  10. while X <= 10000000 do
  11. begin
  12. if BasicIsPrime(X) then
  13. Result := Result + IntToStr(X) + ', ';
  14. Inc(X);
  15. end;
  16. Finish := GetTickCount;
  17. Speed := (Finish - Start) / 1000;
  18. end;
  19.  
  20. function BasicIsPrime(Number: Integer): Boolean;
  21. var
  22. Y: Integer;
  23. begin
  24. if Number <= 1 then
  25. Result := False
  26. else
  27. begin
  28. Y := 2;
  29. Result := True;
  30. while Y <= (Number div 2) do
  31. begin
  32. if Number mod Y = 0 then
  33. begin
  34. Result := False;
  35. Break;
  36. end;
  37. Inc(Y);
  38. end;
  39. end;
  40. end;
 
In respect to the time passed in completing the first example you would expect the second example to be completed in half that time? Let’s see do the maths and find out. In the first example if we were checking all integers from 1 to 10 on both loops then we would be doing 55 iterations of ‘BasicIsPrime’, that is 1+2+3+4+5+6+7+8+9+10 =55. That’s what mathematicians call a triangle number which is defined as (n2+n)/2. So to check from 2 up to ten million we are doing 50,000,004,999,999 iterations – no wonder it took so long!

For example 2 we are actually doing 12,500,002,500,000, which is roughly 3.9 times less. So we have gone from around three hours right down to around forty-six minutes just by simplifying the problem, and we’re not finished there.

For any of you that know about square roots may have already spotted this. Instead of checking the range X/2 in the ‘BasicIsPrime’ function we only need to check up until the square root of X. This is purely because by nature the square root of X is the smallest factor of X which also covers the full range up until X. This further optimisation will reduce or completion time down to a more bearable eighteen minutes, and we haven’t even looked at optimising the actual code itself, only the mathematical algorithm.

Before we look at the actual Delphi code optimisations there is one blindingly obvious issue sticking out like a sore thumb. Why are we checking even numbers after two when none of them can possible be a prime? In fact why are we checking multiples or 2, 3, 5, and 7 once they are gone? To eliminate these issues you should have something like this example and also an algorithm which find all prime numbers from 1 to 10,000,000 in less than a minute (depending on your CPU speed):

Simple Solution - Version3
  1. function BasicIsPrime(Number: Integer): Boolean;
  2. var
  3. Y: Integer;
  4. begin
  5. if Number <= 1 then
  6. Result := False
  7. else if Number <= 3 then
  8. Result := True
  9. else if Number = 5 then
  10. Result := True
  11. else if Number = 7 then
  12. Result := True
  13. else if Number mod 2 = 0 then
  14. Result := False
  15. else if Number mod 3 = 0 then
  16. Result := False
  17. else if Number mod 5 = 0 then
  18. Result := False
  19. else if Number mod 7 = 0 then
  20. Result := False
  21. else
  22. begin
  23. Y := 7;
  24. Result := True;
  25. while Y <= Round(Sqrt(Number)) do
  26. begin
  27. if Number mod Y = 0 then
  28. begin
  29. Result := False;
  30. Break;
  31. end;
  32. Inc(Y, 2);
  33. end;
  34. end;
  35. end;
 
There is only one thing I want to show you before I leave you to attempt to optimise your own algorithms: loop efficiency.

First let’s look at our while loop in our ‘BasicIsPrime’ function and consider the question, is the rounded square root of Number ever going to change in each instance of ‘BasicIsPrime’? The answer is clearly no, however on each iteration of the while loop we are calculating its value again and adding to the completion time unnecessarily. By assigning the rounded square root of Number to a variable before starting the loop we only calculate the value once and therefore shave valuable wasted time off the completion time.

Efficient logic
  1. ...
  2. Sqr := Round(Sqrt(Number));
  3. while Y <= Sqr do
  4. ...
 

Summary

Hopefully now you will have a good understanding of how to implement mathematical solutions involving algorithms by simplifying the math, logic and code to reduce the number of CPU clock cycles required till completion. From this introductory tutorial you should also be able the optimise the 'PrimeCycle' function by implementing some of the techniques demonstrated here and thus further reduce completion time significantly. There are yet more efficient ways to solve this problem for you to investigate on your own which involve number sieves and other mathematical techniques, as well as other programming techniques such as inline ASM.

There are no comments for this item.
Please keep your comments brief and on topic, and remember that this is not a discussion thread.
Name : E-mail :
Title : Website :
Comment(s) :
J! Reactions Commenting Software
General Site License
Copyright © 2006 S. A. DeCaro
 
Next >