Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
84
Task/Prime-triangle/C/prime-triangle-1.c
Normal file
84
Task/Prime-triangle/C/prime-triangle-1.c
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
bool is_prime(unsigned int n) {
|
||||
assert(n < 64);
|
||||
static bool isprime[] = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0,
|
||||
0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1,
|
||||
0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1,
|
||||
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0};
|
||||
return isprime[n];
|
||||
}
|
||||
|
||||
void swap(unsigned int* a, size_t i, size_t j) {
|
||||
unsigned int tmp = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = tmp;
|
||||
}
|
||||
|
||||
bool prime_triangle_row(unsigned int* a, size_t length) {
|
||||
if (length == 2)
|
||||
return is_prime(a[0] + a[1]);
|
||||
for (size_t i = 1; i + 1 < length; i += 2) {
|
||||
if (is_prime(a[0] + a[i])) {
|
||||
swap(a, i, 1);
|
||||
if (prime_triangle_row(a + 1, length - 1))
|
||||
return true;
|
||||
swap(a, i, 1);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int prime_triangle_count(unsigned int* a, size_t length) {
|
||||
int count = 0;
|
||||
if (length == 2) {
|
||||
if (is_prime(a[0] + a[1]))
|
||||
++count;
|
||||
} else {
|
||||
for (size_t i = 1; i + 1 < length; i += 2) {
|
||||
if (is_prime(a[0] + a[i])) {
|
||||
swap(a, i, 1);
|
||||
count += prime_triangle_count(a + 1, length - 1);
|
||||
swap(a, i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void print(unsigned int* a, size_t length) {
|
||||
if (length == 0)
|
||||
return;
|
||||
printf("%2u", a[0]);
|
||||
for (size_t i = 1; i < length; ++i)
|
||||
printf(" %2u", a[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
clock_t start = clock();
|
||||
for (unsigned int n = 2; n < 21; ++n) {
|
||||
unsigned int a[n];
|
||||
for (unsigned int i = 0; i < n; ++i)
|
||||
a[i] = i + 1;
|
||||
if (prime_triangle_row(a, n))
|
||||
print(a, n);
|
||||
}
|
||||
printf("\n");
|
||||
for (unsigned int n = 2; n < 21; ++n) {
|
||||
unsigned int a[n];
|
||||
for (unsigned int i = 0; i < n; ++i)
|
||||
a[i] = i + 1;
|
||||
if (n > 2)
|
||||
printf(" ");
|
||||
printf("%d", prime_triangle_count(a, n));
|
||||
}
|
||||
printf("\n");
|
||||
clock_t end = clock();
|
||||
double duration = (end - start + 0.0) / CLOCKS_PER_SEC;
|
||||
printf("\nElapsed time: %f seconds\n", duration);
|
||||
return 0;
|
||||
}
|
||||
92
Task/Prime-triangle/C/prime-triangle-2.c
Normal file
92
Task/Prime-triangle/C/prime-triangle-2.c
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define GCC_ASM // use GCC's asm for i386. If it does not work, #undef it to use alternative func
|
||||
typedef uint32_t uint;
|
||||
typedef uint64_t ulong;
|
||||
|
||||
#define MASK 0xa08228828228a2bULL
|
||||
|
||||
#ifdef GCC_ASM
|
||||
|
||||
static inline uint
|
||||
bpos(uint x)
|
||||
{
|
||||
uint b;
|
||||
asm("bsf %0, %0" : "=r" (b): "0" (x));
|
||||
return b;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static inline uint
|
||||
bpos(uint x)
|
||||
{
|
||||
static const uint bruijin[32] = {
|
||||
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
|
||||
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
|
||||
};
|
||||
return bruijin[((uint)((x & -x) * 0x077CB531U)) >> 27];
|
||||
}
|
||||
|
||||
#endif // GCC_ASM
|
||||
|
||||
int count(uint n, const uint s, uint avail)
|
||||
{
|
||||
int cnt = 0;
|
||||
|
||||
avail ^= s;
|
||||
if (--n)
|
||||
for (uint b = (uint)(MASK>>bpos(s)) & avail; b; b &= b-1)
|
||||
cnt += count(n, b&-b, avail);
|
||||
else
|
||||
return (MASK & s) != 0;
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
int disp(uint n, const uint s, uint avail, int maxn, uint *seq)
|
||||
{
|
||||
seq[n--] = s;
|
||||
if (!n) {
|
||||
if ((MASK & s)) {
|
||||
for (int i = 0; i < maxn; i++)
|
||||
printf(" %d", bpos(seq[i]) + 1);
|
||||
putchar('\n');
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
for (uint b = (uint)(MASK>>bpos(s)) & (avail ^= s); b; b &= b-1)
|
||||
if (disp(n, b&-b, avail, maxn, seq))
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int chain(uint n, int count_only)
|
||||
{
|
||||
const uint top = 1U<<(n - 1);
|
||||
const uint avail = 2*top - 2;
|
||||
|
||||
if (count_only)
|
||||
return count(n - 1, top, avail);
|
||||
|
||||
uint seq[32];
|
||||
seq[0] = 1;
|
||||
disp(n - 1, top, avail, n, seq);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
for (int n = 2; n < 21; n++)
|
||||
chain(n, 0);
|
||||
putchar('\n');
|
||||
|
||||
for (int n = 2; n < 21; n++)
|
||||
printf("%d ", chain(n, 1));
|
||||
putchar('\n');
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue