Skip to content

Modified 64-bit primality test#2564

Merged
fredrik-johansson merged 6 commits intoflintlib:mainfrom
JASory:reduced-prime
Jan 23, 2026
Merged

Modified 64-bit primality test#2564
fredrik-johansson merged 6 commits intoflintlib:mainfrom
JASory:reduced-prime

Conversation

@JASory
Copy link
Contributor

@JASory JASory commented Jan 22, 2026

This adds a floating-point semiprime check to the primality test, and substantially reduces the size of the witness table to 64Kib which should produce roughly equivalent or superior performance in many applications.


ulong n_nextprime(ulong n, int FLINT_UNUSED(proved));

int n_is_semiprime_k(ulong n);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make this a static function; it doesn't need to be declared in the header file.

#define WITNESS_BASE_HASH_SIZE 32768
/* To keep this file readable, the lookup tables have been
placed in a seprate header file. */
placed in a seperate header file. */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separate

{
// Precomputed multiplicative inverses of the sqrt of k
const double SQRTINV[11] = {
0.7071067811865475,0.5773502691896258,0.5,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change these to hexadecimal float literals? Just to guard against the possibility that a compiler misrounds and that this creates an off-by-one error for one of the numbers we need to detect.

placed in a seperate header file. */
#include "is_prime_tables.h"

int n_is_semiprime_k(ulong n)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest making this function static.

@fredrik-johansson
Copy link
Collaborator

Can you please update the documentation for n_is_prime (in doc/source/ulong_extras.rst) with a brief explanation of the new algorithm? You can also remove (or update) the obsolete comment in line 1863 in is_prime_tables.h.

Also feel free to add yourself to the copyright notices for is_prime.c and is_prime_tables.h

@JASory
Copy link
Contributor Author

JASory commented Jan 22, 2026

Should be good now. I reran Feitsma's set to make sure that the hex literals are sufficient as well.

@fredrik-johansson fredrik-johansson merged commit 64a6d02 into flintlib:main Jan 23, 2026
13 checks passed
@fredrik-johansson
Copy link
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants