Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
116
Task/EKG-sequence-convergence/Ada/ekg-sequence-convergence.adb
Normal file
116
Task/EKG-sequence-convergence/Ada/ekg-sequence-convergence.adb
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
with Ada.Text_IO;
|
||||
with Ada.Containers.Generic_Array_Sort;
|
||||
|
||||
procedure EKG_Sequences is
|
||||
|
||||
type Element_Type is new Integer;
|
||||
|
||||
type Index_Type is new Integer range 1 .. 100;
|
||||
subtype Show_Range is Index_Type range 1 .. 30;
|
||||
|
||||
type Sequence is array (Index_Type range <>) of Element_Type;
|
||||
subtype EKG_Sequence is Sequence (Index_Type);
|
||||
|
||||
function GCD (Left, Right : Element_Type) return Integer is
|
||||
A : Element_Type := Left;
|
||||
B : Element_Type := Right;
|
||||
begin
|
||||
while A /= B loop
|
||||
if A > B
|
||||
then A := A - B;
|
||||
else B := B - A;
|
||||
end if;
|
||||
end loop;
|
||||
return Integer (A);
|
||||
end GCD;
|
||||
|
||||
function Contains (A : Sequence;
|
||||
B : Element_Type;
|
||||
Last : Index_Type) return Boolean
|
||||
is (for some Value of A (A'First .. Last) => Value = B);
|
||||
|
||||
function Are_Same (S, T : EKG_Sequence; Last : Index_Type) return Boolean is
|
||||
S_Copy : Sequence := S (S'First .. Last);
|
||||
T_Copy : Sequence := T (T'First .. Last);
|
||||
procedure Sort is
|
||||
new Ada.Containers.Generic_Array_Sort (Index_Type => Index_Type,
|
||||
Element_Type => Element_Type,
|
||||
Array_Type => Sequence);
|
||||
begin
|
||||
Sort (S_Copy);
|
||||
Sort (T_Copy);
|
||||
return S_Copy = T_Copy;
|
||||
end Are_Same;
|
||||
|
||||
function Create_EKG (Start : Element_Type) return EKG_Sequence is
|
||||
EKG : EKG_Sequence := (1 => 1, 2 => Start, others => 0);
|
||||
begin
|
||||
for N in 3 .. Index_Type'Last loop
|
||||
for I in 2 .. Element_Type'Last loop
|
||||
-- A potential sequence member cannot already have been used
|
||||
-- and must have a factor in common with previous member
|
||||
if not Contains (EKG, I, N)
|
||||
and then GCD (EKG (N - 1), I) > 1
|
||||
then
|
||||
EKG (N) := I;
|
||||
exit;
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
return EKG;
|
||||
end Create_EKG;
|
||||
|
||||
procedure Converge (Seq_A, Seq_B : Sequence;
|
||||
Term : out Index_Type;
|
||||
Do_Converge : out Boolean) is
|
||||
begin
|
||||
for I in 3 .. Index_Type'Last loop
|
||||
if Seq_A (I) = Seq_B (I) and then Are_Same (Seq_A, Seq_B, I) then
|
||||
Do_Converge := True;
|
||||
Term := I;
|
||||
return;
|
||||
end if;
|
||||
end loop;
|
||||
Do_Converge := False;
|
||||
Term := Index_Type'Last;
|
||||
end Converge;
|
||||
|
||||
procedure Put (Seq : Sequence) is
|
||||
use Ada.Text_IO;
|
||||
begin
|
||||
Put ("[");
|
||||
for E of Seq (Show_Range) loop
|
||||
Put (E'Image);
|
||||
end loop;
|
||||
Put ("]");
|
||||
end Put;
|
||||
|
||||
use Ada.Text_IO;
|
||||
EKG_2 : constant EKG_Sequence := Create_EKG (2);
|
||||
EKG_5 : constant EKG_Sequence := Create_EKG (5);
|
||||
EKG_7 : constant EKG_Sequence := Create_EKG (7);
|
||||
EKG_9 : constant EKG_Sequence := Create_EKG (9);
|
||||
EKG_10 : constant EKG_Sequence := Create_EKG (10);
|
||||
begin
|
||||
Put ("EKG( 2): "); Put (EKG_2); New_Line;
|
||||
Put ("EKG( 5): "); Put (EKG_5); New_Line;
|
||||
Put ("EKG( 7): "); Put (EKG_7); New_Line;
|
||||
Put ("EKG( 9): "); Put (EKG_9); New_Line;
|
||||
Put ("EKG(10): "); Put (EKG_10); New_Line;
|
||||
|
||||
-- Now compare EKG5 and EKG7 for convergence
|
||||
declare
|
||||
Term : Index_Type;
|
||||
Do_Converge : Boolean;
|
||||
begin
|
||||
Converge (EKG_5, EKG_7, Term, Do_Converge);
|
||||
New_Line;
|
||||
if Do_Converge then
|
||||
Put_Line ("EKG(5) and EKG(7) converge at term "
|
||||
& Term'Image);
|
||||
else
|
||||
Put_Line ("EKG5(5) and EKG(7) do not converge within "
|
||||
& Term'Image & " terms");
|
||||
end if;
|
||||
end;
|
||||
end EKG_Sequences;
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
ekg: function [start, limit][
|
||||
result: @[1 start]
|
||||
|
||||
while [limit > size result][
|
||||
candidate: 2
|
||||
while ø [
|
||||
if and? [not? contains? result candidate]
|
||||
[1 < gcd @[last result candidate]][
|
||||
'result ++ candidate
|
||||
break
|
||||
]
|
||||
inc 'candidate
|
||||
]
|
||||
]
|
||||
|
||||
return result
|
||||
]
|
||||
|
||||
loop [2 5 7 9 10] 'n ->
|
||||
print ["EKG(" ++ (to :string n) ++ "):" join.with:", " ekg n 10]
|
||||
|
||||
print ""
|
||||
ekg5: ekg 5 100
|
||||
ekg7: ekg 7 100
|
||||
|
||||
converged?: false
|
||||
loop 2..99 'i [
|
||||
if and? -> ekg5\[i] = ekg7\[i]
|
||||
-> equal? sort slice ekg5 0 i sort slice ekg7 0 i [
|
||||
print ["EKG(5) and EKG(7) converge at term" i+1]
|
||||
converged?: true
|
||||
break
|
||||
]
|
||||
]
|
||||
|
||||
unless converged? ->
|
||||
print "EKG(5) and EKG(7) do not converge within 100 terms"
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
// Calculate and show here the first 10 members of EKG[2], EKG[5], EKG[7], EKG[9] and EKG[10]
|
||||
for (const i of [2, 5, 7, 9, 10]) {
|
||||
console.log(`EKG[${i}] = [${ekg(i, 10).join(', ')}]`);
|
||||
}
|
||||
|
||||
console.log("Calculate and show here at which term EKG[5] and EKG[7] converge.");
|
||||
const ekg5 = ekg(5, 100);
|
||||
const ekg7 = ekg(7, 100);
|
||||
|
||||
for (let i = 1; i < ekg5.length; i++) {
|
||||
if (ekg5[i] === ekg7[i] && sameSeq(ekg5, ekg7, i)) {
|
||||
console.log(`EKG[5](${i + 1}) = EKG[7](${i + 1}) = ${ekg5[i]}, and are identical from this term on`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Same last element, and all elements in sequence are identical
|
||||
function sameSeq(seq1, seq2, n) {
|
||||
const list1 = seq1.slice(0, n).sort((a, b) => a - b);
|
||||
const list2 = seq2.slice(0, n).sort((a, b) => a - b);
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
if (list1[i] !== list2[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Without Map to identify seen terms, need to examine list.
|
||||
// Calculating 3000 terms in this manner takes 10 seconds
|
||||
// With Map to identify the seen terms, calculating 3000 terms takes .1 sec.
|
||||
function ekg(two, maxN) {
|
||||
const result = [];
|
||||
result.push(1);
|
||||
result.push(two);
|
||||
|
||||
const seen = new Map();
|
||||
seen.set(1, 1);
|
||||
seen.set(two, 1);
|
||||
|
||||
let minUnseen = two === 2 ? 3 : 2;
|
||||
let prev = two;
|
||||
|
||||
for (let n = 3; n <= maxN; n++) {
|
||||
let test = minUnseen - 1;
|
||||
while (true) {
|
||||
test++;
|
||||
if (!seen.has(test) && gcd(test, prev) > 1) {
|
||||
result.push(test);
|
||||
seen.set(test, n);
|
||||
prev = test;
|
||||
|
||||
if (minUnseen === test) {
|
||||
do {
|
||||
minUnseen++;
|
||||
} while (seen.has(minUnseen));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function gcd(a, b) {
|
||||
if (b === 0) {
|
||||
return a;
|
||||
}
|
||||
return gcd(b, a % b);
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
require "table2"
|
||||
local int = require "int"
|
||||
local fmt = require "fmt"
|
||||
|
||||
local limit = 100
|
||||
local starts = {2, 5, 7, 9, 10}
|
||||
local ekg = {}
|
||||
for i = 1, 5 do ekg[i] = table.rep(limit, 0) end
|
||||
local s = 1
|
||||
for starts as start do
|
||||
ekg[s][1] = 1
|
||||
ekg[s][2] = start
|
||||
for n = 3, limit do
|
||||
local i = 2
|
||||
while true do
|
||||
-- A potential sequence member cannot already have been used
|
||||
-- and must have a factor in common with previous member.
|
||||
if !ekg[s]:slice(1, n):contains(i) and int.gcd(ekg[s][n - 1], i) > 1 then
|
||||
ekg[s][n] = i
|
||||
break
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
fmt.write("EKG(%2d): ", start)
|
||||
fmt.tprint("%2d", ekg[s]:slice(1, 30), 30)
|
||||
s += 1
|
||||
end
|
||||
|
||||
-- Now compare EKG5 and EKG7 for convergence.
|
||||
for i = 3, limit do
|
||||
if ekg[2][i] == ekg[3][i] and
|
||||
table.same(ekg[2]:slice(1, i - 1):sort(), ekg[3]:slice(1, i - 1):sort()) then
|
||||
print($"\nEKG(5) and EKG(7) converge at term {i}.")
|
||||
os.exit()
|
||||
end
|
||||
end
|
||||
print($"\nEKG5(5) and EKG(7) do not converge within {limit} terms.")
|
||||
Loading…
Add table
Add a link
Reference in a new issue