If you have a good computer, you can maybe try to find the last prime number that nobody found yet.
Indeed, you can even win a prize for that!
But you have to find a number with more than millions of digits.
I'm sure you can do it.
For the moment, let's see how to find these numbers in this prime number tutorial.
Arguments are a range: the minimum and the maximum numbers.
For example, I passed 2 as first argument and 100 as second argument, so the result displays all prime numbers between 2 and 100.
Notice that this example works until the max value of an unsigned int: 4,294,967,295.
You are of course free to modify the example to reach a greater value.
/* main.c */
#include "h.h"
int main(int ac, char *av[])
{
t_bp b;
init(ac, av, &b);
debug(&b);
return 0;
}
/* misc.c */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "h.h"
void init(int ac, char *av[], t_bp *b)
{
if (ac < 3)
{
printf("Usage: ./prime-number [min] [max]\n");
exit(1);
}
if (atoi(av[1]) <= 1 || atoi(av[2]) <= 1)
{
fprintf(stderr, "Error: %s\n", "Arguments must be greater than 1");
exit(1);
}
b->numberMin = atoi(av[1]);
b->numberMax = atoi(av[2]);
if ((b->numberMin > b->numberMax) || (b->numberMin == b->numberMax))
{
fprintf(stderr, "Error: %s\n", "Min must be less than Max.");
exit(1);
}
}
void debug(t_bp *b)
{
printf("\n===== START =====\n\n");
prime_testing(b);
printf("\n===== END =====\n\n");
}
void prime_testing(t_bp *b)
{
unsigned int i, j, k;
int pn;
i = b->numberMin;
j = 0;
while (i <= b->numberMax)
{
j = i;
k = 2;
pn = 1;
while (k < j)
{
if (j % k == 0)
{
pn = 0;
}
++k;
}
if (pn > 0)
{
printf("%d\n", j);
}
++i;
}
}
#ifndef H_H_
#define H_H_
/**
* Structure
*/
typedef struct primes
{
unsigned int numberMax;
unsigned int numberMin;
} t_bp;
/**
* Prototype
*/
void init(int ac, char *av[], t_bp *b);
void debug(t_bp *b);
void prime_testing(t_bp *b);
#endif /* H_H_ */
$ gcc main.c misc.c -o prime-numbers ; ./prime-numbers 2 100
===== START ===== 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 ===== END =====
Good job! ![]()
Add new comment