c语言编写 1编写程序用筛选法找素数。2编写程序用6N+1法找素数。 3编写程序用除余法找素数。 4编写程序用遍历二维数组中素数。 5编写程序筛万内的素数--二用筛法。 6编写程序验证哥特巴赫猜想一三用筛法。 7编写程序找费马"二平方"素数一"滚雪球"式地得到所使用的素数。 8编写程序找回文式素数一依然含有诸多猜想。 9编写程序找双向环形素数-循环移位组数判断。 10编写菜单完成程序调用和退出。
时间: 2023-06-19 13:03:35 浏览: 79
1. 筛选法找素数:
```
#include <stdio.h>
int main()
{
int n, i, j, flag;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Prime numbers from 1 to %d are: ", n);
for (i = 2; i <= n; i++) {
flag = 1;
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
printf("%d ", i);
}
}
return 0;
}
```
2. 6N+1法找素数:
```
#include <stdio.h>
#include <math.h>
int main()
{
int n, i, j, flag;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Prime numbers from 1 to %d are: ", n);
for (i = 2; i <= n; i++) {
flag = 1;
if (i == 2 || i == 3) {
printf("%d ", i);
continue;
}
if (i % 6 != 1 && i % 6 != 5) {
continue;
}
for (j = 2; j <= sqrt(i); j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
printf("%d ", i);
}
}
return 0;
}
```
3. 除余法找素数:
```
#include <stdio.h>
#include <math.h>
int main()
{
int n, i, j, flag;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Prime numbers from 1 to %d are: ", n);
for (i = 2; i <= n; i++) {
flag = 1;
for (j = 2; j <= sqrt(i); j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
printf("%d ", i);
}
}
return 0;
}
```
4. 遍历二维数组中素数:
```
#include <stdio.h>
#include <math.h>
int isPrime(int n)
{
int i;
if (n == 2 || n == 3) {
return 1;
}
if (n % 6 != 1 && n % 6 != 5) {
return 0;
}
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main()
{
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int i, j;
printf("Prime numbers in the array: ");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (isPrime(arr[i][j])) {
printf("%d ", arr[i][j]);
}
}
}
return 0;
}
```
5. 筛法找素数:
```
#include <stdio.h>
#include <string.h>
int main()
{
int n, i, j;
printf("Enter the value of n: ");
scanf("%d", &n);
int primes[n + 1];
memset(primes, 1, sizeof(primes));
for (i = 2; i * i <= n; i++) {
if (primes[i]) {
for (j = i * i; j <= n; j += i) {
primes[j] = 0;
}
}
}
printf("Prime numbers from 1 to %d are: ", n);
for (i = 2; i <= n; i++) {
if (primes[i]) {
printf("%d ", i);
}
}
return 0;
}
```
6. 哥特巴赫猜想验证:
```
#include <stdio.h>
#include <string.h>
int isPrime(int n)
{
int i;
if (n == 2 || n == 3) {
return 1;
}
if (n % 6 != 1 && n % 6 != 5) {
return 0;
}
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main()
{
int n, i, j, flag;
printf("Enter the value of n: ");
scanf("%d", &n);
int primes[n + 1];
memset(primes, 1, sizeof(primes));
for (i = 2; i * i <= n; i++) {
if (primes[i]) {
for (j = i * i; j <= n; j += i) {
primes[j] = 0;
}
}
}
printf("Goldbach conjecture numbers from 4 to %d: ", n);
for (i = 4; i <= n; i += 2) {
flag = 0;
for (j = 2; j <= i / 2; j++) {
if (primes[j] && primes[i - j]) {
flag = 1;
printf("(%d,%d) ", j, i - j);
break;
}
}
if (flag == 0) {
printf("Goldbach conjecture is false for %d", i);
break;
}
}
return 0;
}
```
7. 滚雪球式找素数:
```
#include <stdio.h>
#include <string.h>
int main()
{
int n, i, j, k, flag;
printf("Enter the value of n: ");
scanf("%d", &n);
int primes[n + 1];
memset(primes, 1, sizeof(primes));
for (i = 2; i * i <= n; i++) {
if (primes[i]) {
for (j = i * i; j <= n; j += i) {
primes[j] = 0;
}
}
}
printf("Prime numbers from 1 to %d: 2 ", n);
for (i = 1; i <= n / 6; i++) {
for (j = -1; j <= 1; j += 2) {
k = 6 * i + j;
if (k > n) {
break;
}
if (primes[k]) {
printf("%d ", k);
}
}
}
return 0;
}
```
8. 回文式素数:
```
#include <stdio.h>
#include <math.h>
int isPrime(int n)
{
int i;
if (n == 2 || n == 3) {
return 1;
}
if (n % 6 != 1 && n % 6 != 5) {
return 0;
}
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int isPalindrome(int n)
{
int digit = 0, num = n;
while (num > 0) {
digit++;
num /= 10;
}
num = n;
int i, j;
for (i = 0, j = digit - 1; i < j; i++, j--) {
if ((num / (int)pow(10, j)) % 10 != (num / (int)pow(10, i)) % 10) {
return 0;
}
}
return 1;
}
int main()
{
int n, i;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Palindrome prime numbers from 1 to %d: ", n);
for (i = 2; i <= n; i++) {
if (isPrime(i) && isPalindrome(i)) {
printf("%d ", i);
}
}
return 0;
}
```
9. 双向环形素数:
```
#include <stdio.h>
#include <math.h>
int isPrime(int n)
{
int i;
if (n == 2 || n == 3) {
return 1;
}
if (n % 6 != 1 && n % 6 != 5) {
return 0;
}
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int isCircularPrime(int n)
{
int num = n;
int digit = 0;
while (num > 0) {
digit++;
num /= 10;
}
num = n;
int i, j;
for (i = 0; i < digit; i++) {
if (!isPrime(num)) {
return 0;
}
int rem = num % 10;
num = rem * pow(10, digit - 1) + num / 10;
}
return 1;
}
int main()
{
int n, i;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Circular prime numbers from 1 to %d: ", n);
for (i = 2; i <= n; i++) {
if (isCircularPrime(i)) {
printf("%d ", i);
}
}
return 0;
}
```
10. 菜单调用程序:
```
#include <stdio.h>
#include <string.h>
#include <math.h>
int isPrime(int n)
{
int i;
if (n == 2 || n == 3) {
return 1;
}
if (n % 6 != 1 && n % 6 != 5) {
return 0;
}
for (i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
void sieveOfEratosthenes(int n)
{
int primes[n + 1];
memset(primes, 1, sizeof(primes));
int i, j;
for (i = 2; i * i <= n; i++) {
if (primes[i]) {
for (j = i * i; j <= n; j += i) {
primes[j] = 0;
}
}
}
printf("Prime numbers from 1 to %d are: ", n);
for (i = 2; i <= n; i++) {
if (primes[i]) {
printf("%d ", i);
}
}
}
void circularPrimes(int n)
{
int num = n;
int digit = 0;
while (num > 0) {
digit++;
num /= 10;
}
num = n;
int i, j;
for (i = 0; i < digit; i++) {
if (!isPrime(num)) {
printf("%d is not a circular prime", n);
return;
}
int rem = num % 10;
num = rem * pow(10, digit - 1) + num / 10;
}
printf("%d is a circular prime", n);
}
int main()
{
int choice, n;
do {
printf("\n1. Sieve of Eratosthenes\n2. Circular Primes\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value of n: ");
scanf("%d", &n);
sieveOfEratosthenes(n);
break;
case 2:
printf("Enter the value of n: ");
scanf("%d", &n);
circularPrimes(n);
break;
case 3:
printf("Exiting program...");
break;
default:
printf("Invalid choice, please try again.");
break;
}
} while (choice != 3);
return 0;
}
```
阅读全文