I need a C command line program to prints the first prime numbers that are congruent to…
- User
I need a C command line program to prints the first prime numbers that are congruent to 1 module 113.
Assistant
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> // Function to check if a number is prime bool is_prime(unsigned long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; // Check all numbers of the form 6k ± 1 up to sqrt(n) unsigned long long i = 5; while (i * i <= n) { if (n % i == 0 || n % (i + 2) == 0) return false; i += 6; } return true; } int main(int argc, char *argv[]) { int count = 10; // Default to printing first 10 such primes // If argument is provided, use it as the count if (argc > 1) { count = atoi(argv[1]); if (count <= 0) { printf("Please provide a positive number as argument.\n"); return 1; } } printf("First %d prime numbers that are congruent to 1 modulo 113:\n", count); int found = 0; unsigned long long k = 0; while (found < count) { unsigned long long num = 113 * k + 1; if (is_prime(num)) { printf("%llu\n", num); found++; } k++; } return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.