langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
|
|
@ -0,0 +1,4 @@
|
|||
Q=vector(1000);Q[1]=Q[2]=1;for(n=3,#Q,Q[n]=Q[n-Q[n-1]]+Q[n-Q[n-2]]);
|
||||
Q1=vecextract(Q,"1..10");
|
||||
print("First 10 terms: "Q1,if(Q1==[1, 1, 2, 3, 3, 4, 5, 5, 6, 6]," (as expected)"," (in error)"));
|
||||
print("1000-th term: "Q[1000],if(Q[1000]==502," (as expected)"," (in error)"));
|
||||
20
Task/Hofstadter-Q-sequence/PL-I/hofstadter-q-sequence-1.pli
Normal file
20
Task/Hofstadter-Q-sequence/PL-I/hofstadter-q-sequence-1.pli
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* Hofstrader Q sequence for any "n". */
|
||||
|
||||
H: procedure options (main); /* 28 January 2012 */
|
||||
declare n fixed binary(31);
|
||||
|
||||
put ('How many values do you want? :');
|
||||
get (n);
|
||||
|
||||
begin;
|
||||
declare Q(n) fixed binary (31);
|
||||
declare i fixed binary (31);
|
||||
|
||||
Q(1), Q(2) = 1;
|
||||
do i = 1 upthru n;
|
||||
if i >= 3 then Q(i) = ( Q(i - Q(i-1)) + Q(i - Q(i-2)) );
|
||||
if i <= 20 then put skip list ('n=' || trim(i), Q(i));
|
||||
end;
|
||||
put skip list ('n=' || trim(i), Q(i));
|
||||
end;
|
||||
end H;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
declare tally fixed binary (31) initial (0);
|
||||
|
||||
do i = 1 to n-1;
|
||||
if Q(i) > Q(i+1) then tally = tally + 1;
|
||||
end;
|
||||
put skip data (tally);
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
Program HofstadterQSequence (output);
|
||||
|
||||
const
|
||||
limit = 100000;
|
||||
|
||||
var
|
||||
q: array [1..limit] of longint;
|
||||
i, flips: longint;
|
||||
|
||||
begin
|
||||
q[1] := 1;
|
||||
q[2] := 1;
|
||||
for i := 3 to limit do
|
||||
q[i] := q[i - q[i - 1]] + q[i - q[i - 2]];
|
||||
for i := 1 to 10 do
|
||||
write(q[i], ' ');
|
||||
writeln;
|
||||
writeln(q[1000]);
|
||||
flips := 0;
|
||||
for i := 1 to limit - 1 do
|
||||
if q[i] > q[i+1] then
|
||||
inc(flips);
|
||||
writeln('Flips: ', flips);
|
||||
end.
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
class Hofstadter {
|
||||
has @!c = 1,1;
|
||||
multi method postcircumfix:<[ ]> ($me: Int $i) {
|
||||
@!c.push($me[@!c.elems-$me[@!c.elems-1]] +
|
||||
$me[@!c.elems-$me[@!c.elems-2]]) until @!c.exists($i);
|
||||
return @!c[$i];
|
||||
}
|
||||
|
||||
# note that this method isn't strictly required for solving the task,
|
||||
# it's just a wrapper to get more than one item
|
||||
multi method postcircumfix:<[ ]> ($me: Range $is) {
|
||||
return gather {take $me[$_] for $is.iterator;}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
my Hofstadter $Q .= new();
|
||||
|
||||
say "first ten: $Q[^10]";
|
||||
say "1000th: $Q[999]";
|
||||
|
||||
my $count = 0;
|
||||
$count++ if $Q[$_ +1 ] < $Q[$_] for ^99_999;
|
||||
say "In the first 100_000 terms, $count terms are less than their preceding terms";
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
constant @Q = 1, 1, -> $a, $b {
|
||||
(state $n = 1)++;
|
||||
@Q[$n - $a] + @Q[$n - $b]
|
||||
} ... *;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
say "first ten: ", @Q[^10];
|
||||
say "1000th: ", @Q[999];
|
||||
say "In the first 100_000 terms, ",
|
||||
[+](@Q[1..100000] Z< @Q[0..99999]),
|
||||
" terms are less than their preceding terms";
|
||||
38
Task/Hofstadter-Q-sequence/Seed7/hofstadter-q-sequence.seed7
Normal file
38
Task/Hofstadter-Q-sequence/Seed7/hofstadter-q-sequence.seed7
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const type: intHash is hash [integer] integer;
|
||||
|
||||
var intHash: qHash is intHash.value;
|
||||
|
||||
const func integer: q (in integer: n) is func
|
||||
result
|
||||
var integer: q is 1;
|
||||
begin
|
||||
if n in qHash then
|
||||
q := qHash[n];
|
||||
else
|
||||
if n > 2 then
|
||||
q := q(n - q(pred(n))) + q(n - q(n - 2));
|
||||
end if;
|
||||
qHash @:= [n] q;
|
||||
end if;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var integer: n is 0;
|
||||
var integer: less_than_preceding is 0;
|
||||
begin
|
||||
writeln("q(n) for n = 1 .. 10:");
|
||||
for n range 1 to 10 do
|
||||
write(q(n) <& " ");
|
||||
end for;
|
||||
writeln;
|
||||
writeln("q(1000)=" <& q(1000));
|
||||
for n range 2 to 100000 do
|
||||
if q(n) < q(pred(n)) then
|
||||
incr(less_than_preceding);
|
||||
end if;
|
||||
end for;
|
||||
writeln("q(n) < q(n-1) for n = 2 .. 100000: " <& less_than_preceding);
|
||||
end func;
|
||||
13
Task/Hofstadter-Q-sequence/XPL0/hofstadter-q-sequence.xpl0
Normal file
13
Task/Hofstadter-Q-sequence/XPL0/hofstadter-q-sequence.xpl0
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
code ChOut=8, CrLf=9, IntOut=11;
|
||||
int N, C, Q(100_001);
|
||||
[Q(1):= 1; Q(2):= 1; C:= 0;
|
||||
for N:= 3 to 100_000 do
|
||||
[Q(N):= Q(N-Q(N-1)) + Q(N-Q(N-2));
|
||||
if Q(N) < Q(N-1) then C:= C+1;
|
||||
];
|
||||
for N:= 1 to 10 do
|
||||
[IntOut(0, Q(N)); ChOut(0, ^ )];
|
||||
CrLf(0);
|
||||
IntOut(0, Q(1000)); CrLf(0);
|
||||
IntOut(0, C); CrLf(0);
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue