#include <stdio.h>
#include <stdbool.h>
#include <math.h> // Required for sqrt, although i*i <= num is used for efficiency
/**
* @brief Checks if a number is prime using an optimized trial division method.
*
* Optimization:
* 1. Handles base cases (<= 1, 2, 3).
* 2. Skips even numbers (already handled by the main loop structure,
* but good for a general prime check).
* 3. Skips multiples of 3.
* 4. Checks divisors of the form 6k ± 1 starting from 5, up to sqrt(num).
* This eliminates checking multiples of 2 and 3.
*
* @param num The number to check for primality.
* @return true if the number is prime, false otherwise.
*/
bool isPrime(long long num) {
if (num <= 1) {
return false; // 1 and numbers less than 1 are not prime
}
if (num <= 3) {
return true; // 2 and 3 are prime
}
if (num % 2 == 0 || num % 3 == 0) {
return false; // Eliminate multiples of 2 and 3
}
// Check for divisors from 5 up to sqrt(num).
// We can skip checking every other odd number (3, 7, 11, 13, ...)
// by checking numbers of the form 6k ± 1 (5, 7, 11, 13, 17, 19, ...).
// The loop structure i=5, i+=6 checks 5, 11, 17, ... (6k-1 form)
// and i+2 checks 7, 13, 19, ... (6k+1 form).
// Using i * i <= num instead of i <= sqrt(num) avoids floating-point
// computations and potential precision issues.
for (long long i = 5; i * i <= num; i = i + 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return false;
}
}
return true; // If no divisors found, the number is prime
}
int main() {
int count = 0;
// We are looking for prime numbers 'p' such that p ≡ 1 (mod 127).
// This means p can be written as p = 127k + 1 for some integer k.
// If p > 2 is prime, it must be odd.
// 127k + 1 is odd if and only if 127k is even.
// Since 127 is odd, k must be even.
// So, k must be of the form 2m for some integer m >= 0.
// The numbers we need to check are of the form 127 * (2m) + 1 = 254m + 1.
// The first candidate (for m=0) is 254*0 + 1 = 1 (not prime).
// The next candidate (for m=1) is 254*1 + 1 = 255. 255 = 3 * 5 * 17 (not prime).
// The next candidate (for m=2) is 254*2 + 1 = 509. We check if 509 is prime.
// ...and so on. The sequence of numbers to test is 1, 255, 509, 763, 1017, ...
// This is an arithmetic progression: start at 1 (or 255 for positive k), add 254.
// Start checking from the first relevant candidate, which is 254*1 + 1 = 255
// (since 1 is not prime, and we need k > 0).
// Alternatively, we can start with k=0, check 1, then k=2, check 255, etc.
// Let's start with the first potential prime > 1, which corresponds to m=1.
long long current_candidate = 254 * 1 + 1; // Start with 255 (k=2, m=1)
printf("First 100 prime numbers congruent to 1 mod 127:\n");
// If the first candidate (1) were considered prime (it's not),
// the loop below would start from k=0.
// Since 1 is not prime, we start checking numbers derived from k >= 2 (even k).
// current_candidate effectively starts at 255 (k=2, m=1), then becomes 509 (k=4, m=2), etc.
while (count < 100) {
// The number `current_candidate` is always of the form 254m + 1,
// which is equivalent to 127k + 1 where k=2m is even.
// These are the only numbers > 2 that can be prime and congruent to 1 mod 127.
if (isPrime(current_candidate)) {
printf("%lld\n", current_candidate);
count++;
}
// Move to the next candidate number of the form 254m + 1.
// This is achieved by adding 254.
// If current_candidate was 254m + 1, the next is 254(m+1) + 1 = 254m + 254 + 1.
current_candidate += 254;
}
return 0;
}
Explanation and Optimization Used:
- Target Form: We are looking for prime numbers
p such that p ≡ 1 (mod 127). This means p can be written as p = 127k + 1 for some integer k.
- Eliminating Composites Based on Form:
- If a prime
p > 2 is of the form 127k + 1, it must be odd.
127k + 1 is odd if and only if 127k is even.
- Since
127 is odd, k must be even.
- So, we only need to check numbers where
k is even. Let k = 2m for some integer m ≥ 0.
- The numbers we need to check are thus of the form
127 * (2m) + 1 = 254m + 1.
- This is a significant optimization: we only check numbers in an arithmetic progression with a difference of 254, skipping all the numbers of the form
127k + 1 where k is odd (which we know are even and thus not prime, except for 2, which cannot be expressed as 127k+1 for integer k).
- Efficient Primality Test (
isPrime function):
- Base Cases: Handles
0, 1, 2, 3 directly.
- Divisibility by 2 and 3: Quickly eliminates numbers divisible by 2 or 3. Note that numbers of the form
254m + 1 are never divisible by 2 (as 254 is even and 1 is odd), but the check for 3 is useful (e.g., 255 = 254*1+1 is divisible by 3).
- Trial Division Optimization: Instead of checking all odd numbers
5, 7, 9, 11, 13, 15, 17, ... as potential divisors, we only check numbers of the form 6k ± 1 (5, 7, 11, 13, 17, 19, ...). This is because any prime greater than 3 can be written in this form. This optimization skips 2/3 of the potential trial divisors compared to checking every odd number after 3.
- Limit of Trial Division: We only need to check for divisors up to the square root of the number being tested (
i * i <= num). If a number num has a divisor larger than sqrt(num), it must also have a divisor smaller than sqrt(num).
- Integer Arithmetic: Using
i * i <= num is preferred over i <= sqrt(num) to avoid potential floating-point inaccuracies and overhead. long long is used for num and i to handle larger numbers that occur as we search for the 100th prime.
This approach efficiently generates only the numbers that could possibly be primes of the required form and uses a standard, well-optimized trial division for primality testing.