What techniques can speed up counting pseudoprimes?

  • Context: Graduate 
  • Thread starter Thread starter CRGreathouse
  • Start date Start date
  • Tags Tags
    Counting
Click For Summary
SUMMARY

The discussion focuses on optimizing the calculation of k-strong pseudoprimes using the Pari/GP programming language. The provided naive function, ff(n), calculates the count of k-strong pseudoprimes for composite integers up to one million, but is inefficient, taking approximately 10 seconds for large inputs. A significant optimization is introduced by leveraging the property that ff(n) is at most phi(n)/4, which allows for substantial reductions in the number of checks required, improving performance by skipping up to 90% of numbers in certain ranges. The discussion invites further techniques to enhance this calculation.

PREREQUISITES
  • Understanding of k-strong pseudoprimes and their properties
  • Familiarity with the Pari/GP programming language
  • Knowledge of the Euler's totient function, phi(n)
  • Basic concepts of number theory related to composite integers
NEXT STEPS
  • Explore advanced algorithms for pseudoprime testing, such as the Baillie-PSW primality test
  • Learn about optimizing algorithms in Pari/GP for performance enhancements
  • Investigate mathematical properties of pseudoprimes to identify further optimization strategies
  • Study the implications of skipping numbers based on their abundance and smoothness
USEFUL FOR

Mathematicians, computer scientists, and programmers interested in number theory, particularly those focused on optimizing algorithms for primality testing and pseudoprime calculations.

CRGreathouse
Science Advisor
Homework Helper
Messages
2,832
Reaction score
0
I'm calculating the high-water marks for the following function:
f(n) = #{n is a k-strong pseudoprime, 1 < k < n}
where n is a composite integer.

The naive Pari code:
Code:
ff(n)=sum(k=2,n-1,isSPRP(n, k))

record=0;forstep(n=3, 1e6, 2, if(isprime(n), next); k = ff(n); if (k > record, record = k; print (k" "n)))

Note: I'm using the following straightforward code for checking if a number is a strong pseudoprime; here's the code if you want to duplicate my above functions. I'm not looking for improvements to this part, though if you have any I'd love to hear about it.
Code:
isSPRP(n,b)={
	my(d = n, s = 0);
	until(bitand(d,1), d >>= 1; s++);
	d = Mod(b, n)^d;
	if (d == 1, return(1));
	for(i=1,s-1,
		if (d == -1, return(1));
		d = d^2;
	);
	d == -1
};
But this is very slow: checking ff for a number around a million takes about 10 seconds, so I'm looking at weeks or months of calculation even to check to a million (and I'd like to check more than that).

'Everyone' knows that a composite number can't be a strong pseudoprime to more than a quarter of the k, but more is true: ff(n) is at most phi(n)/4. This simple test significantly speeds results, allowing me to skip between 20% and 90% of the numbers in the range (the relatively smooth/abundant ones) -- the further closer together the records are, the more I can skip. Are there any other ideas?
 
Physics news on Phys.org

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 97 ·
4
Replies
97
Views
10K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
5
Views
3K
  • · Replies 45 ·
2
Replies
45
Views
7K