sssms2

Unnamed repository; edit this file 'description' to name the repository.
git clone https://git.inz.fi/sssms2/
Log | Files | Refs

commit 268d626041544256a04b10fe0dc0bd69c3218dd0
Author: Santtu Lakkala <inz@inz.fi>
Date:   Tue,  4 Jan 2022 14:19:58 +0200

Initial import

Diffstat:
AMakefile | 24++++++++++++++++++++++++
Asssms2.c | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest/test.output | 38++++++++++++++++++++++++++++++++++++++
3 files changed, 169 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,24 @@ +TARGET := sssms2 +CFLAGS := -O3 -pthread +LDFLAGS := -lm -pthread +SOURCES := sssms2.c +OBJS := ${SOURCES:.c=.o} + + +${TARGET}: ${OBJS} + ${CC} -o ${TARGET} ${OBJS} ${LDFLAGS} + +.c.o: + ${CC} ${CFLAGS} -c $< -o $@ + +test: ${TARGET} + @time ./${TARGET} 1 50000 | diff -u test/test.output - + +clean: + rm -f ${OBJS} + +distclean: clean + rm ${TARGET} *~ + +.POSIX: +.PHONY: test diff --git a/sssms2.c b/sssms2.c @@ -0,0 +1,107 @@ +/* + * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + * Version 2, December 2004 + * + * Copyright (C) 2021 Santtu Lakkala <inz@inz.fi> + * + * Everyone is permitted to copy and distribute verbatim or modified + * copies of this license document, and changing it is allowed as long + * as the name is changed. + * + * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + * + * 0. You just DO WHAT THE FUCK YOU WANT TO. + * + * compile with something like: + * cc -std=c11 sssms2.c -o sssms2 -lm -pthread + */ +#include <stdint.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <inttypes.h> +#include <stdatomic.h> +#include <pthread.h> + +static const uint8_t f2[8][8] = { + { 19, 1, 0, 0, 17, 0, 0, 0 }, + { 1, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 136, 0, 0, 0, 136 }, + { 0, 0, 136, 68, 0, 0, 136, 68 }, + { 17, 0, 0, 0, 17, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 136, 0, 0, 0, 136 }, + { 0, 0, 136, 68, 0, 0, 136, 196 } +}; + +static inline bool issquare(uint64_t x) +{ + uint64_t sqr = sqrtl(x); + return sqr * sqr == x; +} + +static _Atomic uint64_t ga = ATOMIC_VAR_INIT(1); +static uint64_t high; + +void *worker(void *data) +{ + uint64_t a; + uint64_t b; + uint64_t c; + + (void)data; + + while ((a = ga++) < high) { + if ((a & 7) == 5) + continue; + + for (b = 1; b <= a; b++) { + const uint8_t f3 = f2[a & 7][b & 7]; + if (f3) { + uint64_t a2b2 = a * a + b * b; + uint64_t mrc = sqrtl(a2b2 + b); + uint64_t rc; + + for (rc = sqrt(a2b2 - 1) + 1; + rc <= mrc; rc++) { + c = rc * rc - a2b2; + + if (f3 & (1 << (c & 7)) && + issquare(a * a + b + c * c) && + issquare(a + b * b + c * c)) + printf("%" PRIu64 " %" PRIu64 " %" PRIu64 "\n", c, b, a); + } + } + } + } + + return NULL; +} + +int main(int argc, char **argv) +{ + int t = 1; + int i; + high = 5000; + + if (argc >= 3) { + ga = strtoull(argv[1], NULL, 10); + high = strtoull(argv[2], NULL, 10); + + if (argc >= 4) + t = atoi(argv[3]); + } + + pthread_t threads[t - 1]; + + for (i = 0; i < t - 1; i++) + pthread_create(&threads[i], NULL, worker, NULL); + worker(NULL); + + for (i = 0; i < t - 1; i++) + pthread_join(threads[i], NULL); + + return 0; +} diff --git a/test/test.output b/test/test.output @@ -0,0 +1,38 @@ +4 4 4 +24 24 73 +31 135 135 +144 144 144 +63 63 162 +137 224 224 +136 136 257 +48 48 292 +68 304 304 +160 240 321 +388 516 516 +376 376 1337 +495 495 1351 +935 935 1879 +959 959 2087 +607 1791 3534 +304 3449 4352 +3649 4864 4864 +4900 4900 4900 +868 868 6052 +5138 6467 6467 +1921 7240 7240 +1424 8720 8720 +336 336 9433 +2761 5856 9768 +8439 8439 10783 +9697 12208 12208 +2480 2480 12281 +1230 6051 15475 +1859 1859 15662 +14180 14180 18116 +5430 9595 23751 +15599 15599 30119 +16712 16712 31337 +19876 24036 31428 +11192 11192 35513 +1447 7034 38891 +9088 9088 41828