RosettaCodeData/Task/Stern-Brocot-sequence/XPL0/stern-brocot-sequence.xpl0
2024-10-16 18:07:41 -07:00

40 lines
1,009 B
Text

func GCD(N, D); \Return the greatest common divisor of N and D
int N, D; \numerator and denominator
int R;
[if D > N then
[R:=D; D:=N; N:=R]; \swap D and N
while D > 0 do
[R:= rem(N/D);
N:= D;
D:= R;
];
return N;
];
int Seq(1200), Con, I, N;
[Seq(0):= 1; Seq(1):= 1; Con:= 1; I:= 2;
repeat Seq(I):= Seq(Con) + Seq(Con-1);
I:= I+1;
Seq(I):= Seq(Con);
I:= I+1;
Con:= Con+1;
until I >= 1200;
Text(0, "First 15 members of the Stern-Brocot sequence:^m^j");
for I:= 0 to 15-1 do
[IntOut(0, Seq(I)); ChOut(0, ^ )];
CrLf(0);
N:= 1; I:= 0;
repeat if Seq(I) = N then
[if N <= 10 or N = 100 then
[Text(0, "First "); IntOut(0, N);
Text(0, " at "); IntOut(0, I+1); CrLf(0);
];
N:= N+1;
I:= 0;
];
I:= I+1;
until N > 100;
for I:= 0 to 1000-1 do
if GCD(Seq(I), Seq(I+1)) # 1 then return;
Text(0, "All GCD are 1^m^j");
]