~fmac/f292

b89b4e70fdb3d4bca2b32c42df0623f16f0e8020 — Francesco Camuffo 5 months ago
Initial commit
3 files changed, 117 insertions(+), 0 deletions(-)

A LICENSE
A README
A main.c
A  => LICENSE +21 -0
@@ 1,21 @@
MIT License

Copyright (c) 2020 Francesco Camuffo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A  => README +31 -0
@@ 1,31 @@
# f292

C implementation of the [Prime-Representing Constant](https://en.wikipedia.org/wiki/Formula_for_primes#A_function_that_represents_all_primes) (A function that represents all primes).
Also knows as `2.920050977316134712092562917112019`.

---

Uses [GMP](https://gmplib.org/) to handle big floating point numbers.

---

## Build

``` sh
gcc main.c -lgmp -o f292
./f292
```

---

## Usage

```
f292 [-n <count>] [-p <precision>]

Options:

    -n  number of primes to generate

    -p  number of bits of precision to be set with mpf_set_default_prec
```

A  => main.c +65 -0
@@ 1,65 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gmp.h>

char c_str[] = "2.920050977316134712092562917112019";

void
gen_primes(mpf_t start, unsigned int count)
{
	mpf_t n, flr, n_flr, n_flr_1;
	mpf_init_set(n, start);
	mpf_init(flr);
	mpf_init(n_flr);
	mpf_init(n_flr_1);
	unsigned int i = 0;

	while (i < count) {
		mpf_floor(flr, n);
		gmp_printf("%.Ff\n", flr);

		mpf_sub(n_flr, n, flr);
		mpf_add_ui(n_flr_1, n_flr, 1);
		mpf_mul(n, flr, n_flr_1);

		i++;
	}
}

int
main(int argc, char *argv[])
{
	unsigned int count = 10;
	unsigned int prec = 256;

	int opt;
	while ((opt = getopt(argc, argv, "n:p:")) != -1) {
		switch (opt)
		{
			case 'n':
				count = atoi(optarg);
				break;
			case 'p':
				prec = atoi(optarg);
				break;
			case '?':
				fprintf(stderr,
						"Usage: %s [-n <count>] [-p <precision>]\n",
						argv[0]);
				exit(1);
			default:
				abort();
		}
	}

	mpf_set_default_prec(prec);
	mpf_t c;
	mpf_init(c);
	mpf_set_str(c, c_str, 10);

	gen_primes(c, count);

	return 0;
}