Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
17
Task/Pythagorean-triples/Clojure/pythagorean-triples-1.clj
Normal file
17
Task/Pythagorean-triples/Clojure/pythagorean-triples-1.clj
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(defn gcd [a b] (if (zero? b) a (recur b (mod a b))))
|
||||
|
||||
(defn pyth [peri]
|
||||
(for [m (range 2 (Math/sqrt (/ peri 2)))
|
||||
n (range (inc (mod m 2)) m 2) ; n<m, opposite polarity
|
||||
:let [p (* 2 m (+ m n))] ; = a+b+c for this (m,n)
|
||||
:while (<= p peri)
|
||||
:when (= 1 (gcd m n))
|
||||
:let [m2 (* m m), n2 (* n n),
|
||||
[a b] (sort [(- m2 n2) (* 2 m n)]), c (+ m2 n2)]
|
||||
k (range 1 (inc (quot peri p)))]
|
||||
[(= k 1) (* k a) (* k b) (* k c)]))
|
||||
|
||||
(defn rcount [ts] ; (->> peri pyth rcount) produces [total, primitive] counts
|
||||
(reduce (fn [[total prims] t] [(inc total), (if (first t) (inc prims) prims)])
|
||||
[0 0]
|
||||
ts))
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
(defn pyth-count [peri]
|
||||
(reduce (fn [[total prims] k] [(+ total k), (inc prims)]) [0 0]
|
||||
(for [m (range 2 (Math/sqrt (/ peri 2)))
|
||||
n (range (inc (mod m 2)) m 2) ; n<m, opposite polarity
|
||||
:let [p (* 2 m (+ m n))] ; = a+b+c for this (m,n)
|
||||
:while (<= p peri)
|
||||
:when (= 1 (gcd m n))]
|
||||
(quot peri p))))
|
||||
|
|
@ -1,18 +1,5 @@
|
|||
my %triples;
|
||||
my $limit = 100;
|
||||
constant limit = 20;
|
||||
|
||||
for 3 .. $limit/2 -> $c {
|
||||
for 1 .. $c -> $a {
|
||||
my $b = ($c * $c - $a * $a).sqrt;
|
||||
last if $c + $a + $b > $limit;
|
||||
last if $a > $b;
|
||||
if $b == $b.Int {
|
||||
my $key = "$a $b $c";
|
||||
%triples{$key} = ([gcd] $c, $a, $b.Int) > 1 ?? 0 !! 1;
|
||||
say $key, %triples{$key} ?? ' - primitive' !! '';
|
||||
}
|
||||
}
|
||||
for [X] [^limit] xx 3 -> \a, \b, \c {
|
||||
say [a, b, c] if a < b < c and a**2 + b**2 == c**2
|
||||
}
|
||||
|
||||
say "There are {+%triples.keys} Pythagorean Triples with a perimeter <= $limit,"
|
||||
~"\nof which {[+] %triples.values} are primitive.";
|
||||
|
|
|
|||
|
|
@ -1,20 +1,18 @@
|
|||
sub triples($limit) {
|
||||
my $primitive = 0;
|
||||
my $civilized = 0;
|
||||
my %triples;
|
||||
my $limit = 100;
|
||||
|
||||
sub oyako($a, $b, $c) {
|
||||
my $perim = $a + $b + $c;
|
||||
return if $perim > $limit;
|
||||
++$primitive; $civilized += $limit div $perim;
|
||||
oyako( $a - 2*$b + 2*$c, 2*$a - $b + 2*$c, 2*$a - 2*$b + 3*$c);
|
||||
oyako( $a + 2*$b + 2*$c, 2*$a + $b + 2*$c, 2*$a + 2*$b + 3*$c);
|
||||
oyako(-$a + 2*$b + 2*$c, -2*$a + $b + 2*$c, -2*$a + 2*$b + 3*$c);
|
||||
}
|
||||
|
||||
oyako(3,4,5);
|
||||
"$limit => ($primitive $civilized)";
|
||||
for 3 .. $limit/2 -> $c {
|
||||
for 1 .. $c -> $a {
|
||||
my $b = ($c * $c - $a * $a).sqrt;
|
||||
last if $c + $a + $b > $limit;
|
||||
last if $a > $b;
|
||||
if $b == $b.Int {
|
||||
my $key = "$a $b $c";
|
||||
%triples{$key} = ([gcd] $c, $a, $b.Int) > 1 ?? 0 !! 1;
|
||||
say $key, %triples{$key} ?? ' - primitive' !! '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for 10,100,1000 ... * -> $limit {
|
||||
say triples $limit;
|
||||
}
|
||||
say "There are {+%triples.keys} Pythagorean Triples with a perimeter <= $limit,"
|
||||
~"\nof which {[+] %triples.values} are primitive.";
|
||||
|
|
|
|||
|
|
@ -1,21 +1,18 @@
|
|||
constant @coeff = [[+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]];
|
||||
|
||||
sub triples($limit) {
|
||||
my $primitive = 0;
|
||||
my $civilized = 0;
|
||||
|
||||
sub oyako(@trippy) {
|
||||
my $perim = [+] @trippy;
|
||||
sub oyako($a, $b, $c) {
|
||||
my $perim = $a + $b + $c;
|
||||
return if $perim > $limit;
|
||||
take (1 + ($limit div $perim)i);
|
||||
for @coeff -> @nine {
|
||||
oyako (map -> @three { [+] @three »*« @trippy }, @nine);
|
||||
}
|
||||
return;
|
||||
++$primitive; $civilized += $limit div $perim;
|
||||
oyako( $a - 2*$b + 2*$c, 2*$a - $b + 2*$c, 2*$a - 2*$b + 3*$c);
|
||||
oyako( $a + 2*$b + 2*$c, 2*$a + $b + 2*$c, 2*$a + 2*$b + 3*$c);
|
||||
oyako(-$a + 2*$b + 2*$c, -2*$a + $b + 2*$c, -2*$a + 2*$b + 3*$c);
|
||||
}
|
||||
|
||||
my $complex = 0i + [+] gather oyako([3,4,5]);
|
||||
"$limit => ({$complex.re, $complex.im})";
|
||||
oyako(3,4,5);
|
||||
"$limit => ($primitive $civilized)";
|
||||
}
|
||||
|
||||
for 10,100,1000 ... * -> $limit {
|
||||
|
|
|
|||
23
Task/Pythagorean-triples/Perl-6/pythagorean-triples-4.pl6
Normal file
23
Task/Pythagorean-triples/Perl-6/pythagorean-triples-4.pl6
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
constant @coeff = [[+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]];
|
||||
|
||||
sub triples($limit) {
|
||||
|
||||
sub oyako(@trippy) {
|
||||
my $perim = [+] @trippy;
|
||||
return if $perim > $limit;
|
||||
take (1 + ($limit div $perim)i);
|
||||
for @coeff -> @nine {
|
||||
oyako (map -> @three { [+] @three »*« @trippy }, @nine);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
my $complex = 0i + [+] gather oyako([3,4,5]);
|
||||
"$limit => ({$complex.re, $complex.im})";
|
||||
}
|
||||
|
||||
for 10,100,1000 ... * -> $limit {
|
||||
say triples $limit;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue