tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
46
Task/Pythagorean-triples/C/pythagorean-triples-1.c
Normal file
46
Task/Pythagorean-triples/C/pythagorean-triples-1.c
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef unsigned long long xint;
|
||||
typedef unsigned long ulong;
|
||||
|
||||
inline ulong gcd(ulong m, ulong n)
|
||||
{
|
||||
ulong t;
|
||||
while (n) { t = n; n = m % n; m = t; }
|
||||
return m;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ulong a, b, c, pytha = 0, prim = 0, max_p = 100;
|
||||
xint aa, bb, cc;
|
||||
|
||||
for (a = 1; a <= max_p / 3; a++) {
|
||||
aa = (xint)a * a;
|
||||
printf("a = %lu\r", a); /* show that we are working */
|
||||
fflush(stdout);
|
||||
|
||||
/* max_p/2: valid limit, because one side of triangle
|
||||
* must be less than the sum of the other two
|
||||
*/
|
||||
for (b = a + 1; b < max_p/2; b++) {
|
||||
bb = (xint)b * b;
|
||||
for (c = b + 1; c < max_p/2; c++) {
|
||||
cc = (xint)c * c;
|
||||
if (aa + bb < cc) break;
|
||||
if (a + b + c > max_p) break;
|
||||
|
||||
if (aa + bb == cc) {
|
||||
pytha++;
|
||||
if (gcd(a, b) == 1) prim++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("Up to %lu, there are %lu triples, of which %lu are primitive\n",
|
||||
max_p, pytha, prim);
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
Task/Pythagorean-triples/C/pythagorean-triples-2.c
Normal file
1
Task/Pythagorean-triples/C/pythagorean-triples-2.c
Normal file
|
|
@ -0,0 +1 @@
|
|||
Up to 100, there are 17 triples, of which 7 are primitive
|
||||
48
Task/Pythagorean-triples/C/pythagorean-triples-3.c
Normal file
48
Task/Pythagorean-triples/C/pythagorean-triples-3.c
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* should be 64-bit integers if going over 1 billion */
|
||||
typedef unsigned long xint;
|
||||
#define FMT "%lu"
|
||||
|
||||
xint total, prim, max_peri;
|
||||
xint U[][9] = {{ 1, -2, 2, 2, -1, 2, 2, -2, 3},
|
||||
{ 1, 2, 2, 2, 1, 2, 2, 2, 3},
|
||||
{-1, 2, 2, -2, 1, 2, -2, 2, 3}};
|
||||
|
||||
void new_tri(xint in[])
|
||||
{
|
||||
int i;
|
||||
xint t[3], p = in[0] + in[1] + in[2];
|
||||
|
||||
if (p > max_peri) return;
|
||||
|
||||
prim ++;
|
||||
|
||||
/* for every primitive triangle, its multiples would be right-angled too;
|
||||
* count them up to the max perimeter */
|
||||
total += max_peri / p;
|
||||
|
||||
/* recursively produce next tier by multiplying the matrices */
|
||||
for (i = 0; i < 3; i++) {
|
||||
t[0] = U[i][0] * in[0] + U[i][1] * in[1] + U[i][2] * in[2];
|
||||
t[1] = U[i][3] * in[0] + U[i][4] * in[1] + U[i][5] * in[2];
|
||||
t[2] = U[i][6] * in[0] + U[i][7] * in[1] + U[i][8] * in[2];
|
||||
new_tri(t);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
xint seed[3] = {3, 4, 5};
|
||||
|
||||
for (max_peri = 10; max_peri <= 100000000; max_peri *= 10) {
|
||||
total = prim = 0;
|
||||
new_tri(seed);
|
||||
|
||||
printf( "Up to "FMT": "FMT" triples, "FMT" primitives.\n",
|
||||
max_peri, total, prim);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
8
Task/Pythagorean-triples/C/pythagorean-triples-4.c
Normal file
8
Task/Pythagorean-triples/C/pythagorean-triples-4.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
Up to 10: 0 triples, 0 primitives.
|
||||
Up to 100: 17 triples, 7 primitives.
|
||||
Up to 1000: 325 triples, 70 primitives.
|
||||
Up to 10000: 4858 triples, 703 primitives.
|
||||
Up to 100000: 64741 triples, 7026 primitives.
|
||||
Up to 1000000: 808950 triples, 70229 primitives.
|
||||
Up to 10000000: 9706567 triples, 702309 primitives.
|
||||
Up to 100000000: 113236940 triples, 7023027 primitives.
|
||||
51
Task/Pythagorean-triples/C/pythagorean-triples-5.c
Normal file
51
Task/Pythagorean-triples/C/pythagorean-triples-5.c
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* should be 64-bit integers if going over 1 billion */
|
||||
typedef unsigned long xint;
|
||||
#define FMT "%lu"
|
||||
|
||||
xint total, prim, max_peri;
|
||||
|
||||
void new_tri(xint in[])
|
||||
{
|
||||
int i;
|
||||
xint t[3], p;
|
||||
xint x = in[0], y = in[1], z = in[2];
|
||||
|
||||
recur: p = x + y + z;
|
||||
if (p > max_peri) return;
|
||||
|
||||
prim ++;
|
||||
total += max_peri / p;
|
||||
|
||||
t[0] = x - 2 * y + 2 * z;
|
||||
t[1] = 2 * x - y + 2 * z;
|
||||
t[2] = t[1] - y + z;
|
||||
new_tri(t);
|
||||
|
||||
t[0] += 4 * y;
|
||||
t[1] += 2 * y;
|
||||
t[2] += 4 * y;
|
||||
new_tri(t);
|
||||
|
||||
z = t[2] - 4 * x;
|
||||
y = t[1] - 4 * x;
|
||||
x = t[0] - 2 * x;
|
||||
goto recur;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
xint seed[3] = {3, 4, 5};
|
||||
|
||||
for (max_peri = 10; max_peri <= 100000000; max_peri *= 10) {
|
||||
total = prim = 0;
|
||||
new_tri(seed);
|
||||
|
||||
printf( "Up to "FMT": "FMT" triples, "FMT" primitives.\n",
|
||||
max_peri, total, prim);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue