Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COBOL Optimization Discussion

Status
Not open for further replies.
Glenn,

Interesting, but perhaps the computation of prime numbers, while interesting, is not too applicable to 'everyday COBOL.'

Is it your intention to start an optimization discussion here?

Tom Morrison
 
Tom -

No, not a discussion per se, but I thought others might find the types of optimizations attempted and the results achieved useful if/when they are confronted with poorly performing code/algorithms in their own work.

Glenn
 
Nice to see.

This might be interesting if there always is a 'Friday trivia question'.

I quickly browsed on this forum but most entries seems to be HP specific (which is not my kind of brand).

So, does anybody know if there are more code examples like this collected at one central(!) place?

Regards, Wim.
 
If you are talking about optimizing this particular function, there is much to go.

First, the test divisor limit should be the square root of the test number, not half of it.

Next, only prime numbers should be used as test divisors: If a number is not divisible by 3, it certainly is not divisible by 9, or any other multiple of 3.

The optimization increase from the first the the second version is primarily a result of converting to binary work fields. Most computers do binary directly, display computations require subroutines or conversion for most computers.
 
on this specific subject, webrabbit is right. The highest prime factor of a number is always less than or equal to the square root of the number. So in the absence of the formula that has been eluding mathematicians for hundreds of years, the easiest way to go is as follows:
Code:
Make a linked list to hold the 'found primes'
add '2' to the linked list (the first prime)
for n = 3 to [i]n[/i] by +2
   take square root of [i]n[/i]
   loop through the entries in the linked list, dividing into [i]n[/i]
   if linked list number > sqrt([i]n[/i]) or remainder = 0
      then it's not prime, iterate to next
   else
      add it to the linked list
   end-if
Calculating the primes is reasonably fast, and using the two heuristics of the square root limit and only dividing by the primes you have found so far stops pointless calculations.

If you save the primes to a file for subsequent loading into memory by another program, you can use a similar scheme for either finding prime factors or determining if a number is prime. Assuming you use a 4-byte integer, you can find all the primes up to 2 billion pretty quickly. But the square root rule means you can use this generated list of primes to factor numbers much larger than this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top