Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
59
Task/Hofstadter-Q-sequence/Ada/hofstadter-q-sequence.adb
Normal file
59
Task/Hofstadter-Q-sequence/Ada/hofstadter-q-sequence.adb
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Hofstadter_Q_Sequence is
|
||||
|
||||
type Callback is access procedure(N: Positive);
|
||||
|
||||
procedure Q(First, Last: Positive; Q_Proc: Callback) is
|
||||
-- calls Q_Proc(Q(First)); Q_Proc(Q(First+1)); ... Q_Proc(Q(Last));
|
||||
-- precondition: Last > 2
|
||||
|
||||
Q_Store: array(1 .. Last) of Natural := (1 => 1, 2 => 1, others => 0);
|
||||
-- "global" array to store the Q(I)
|
||||
-- if Q_Store(I)=0, we compute Q(I) and update Q_Store(I)
|
||||
-- else we already know Q(I) = Q_Store(I)
|
||||
|
||||
function Q(N: Positive) return Positive is
|
||||
begin
|
||||
if Q_Store(N) = 0 then
|
||||
Q_Store(N) := Q(N - Q(N-1)) + Q(N-Q(N-2));
|
||||
end if;
|
||||
return Q_Store(N);
|
||||
end Q;
|
||||
|
||||
begin
|
||||
for I in First .. Last loop
|
||||
Q_Proc(Q(I));
|
||||
end loop;
|
||||
end Q;
|
||||
|
||||
procedure Print(P: Positive) is
|
||||
begin
|
||||
Ada.Text_IO.Put(Positive'Image(P));
|
||||
end Print;
|
||||
|
||||
Decrease_Counter: Natural := 0;
|
||||
Previous_Value: Positive := 1;
|
||||
|
||||
procedure Decrease_Count(P: Positive) is
|
||||
begin
|
||||
if P < Previous_Value then
|
||||
Decrease_Counter := Decrease_Counter + 1;
|
||||
end if;
|
||||
Previous_Value := P;
|
||||
end Decrease_Count;
|
||||
|
||||
begin
|
||||
Q(1, 10, Print'Access);
|
||||
-- the first ten terms of the sequence are: 1, 1, 2, 3, 3, 4, 5, 5, 6, and 6
|
||||
Ada.Text_IO.New_Line;
|
||||
|
||||
Q(1000, 1000, Print'Access);
|
||||
-- the 1000'th term is: 502
|
||||
Ada.Text_IO.New_Line;
|
||||
|
||||
Q(2, 100_000, Decrease_Count'Access);
|
||||
Ada.Text_IO.Put_Line(Integer'Image(Decrease_Counter));
|
||||
-- how many times a member of the sequence is less than its preceding term
|
||||
-- for terms up to and including the 100,000'th term
|
||||
end Hofstadter_Q_Sequence;
|
||||
40
Task/Hofstadter-Q-sequence/COBOL/hofstadter-q-sequence.cob
Normal file
40
Task/Hofstadter-Q-sequence/COBOL/hofstadter-q-sequence.cob
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. Q-SEQ.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 SEQ.
|
||||
02 Q PIC 9(3) OCCURS 1000 TIMES.
|
||||
02 Q-TMP1 PIC 9(3).
|
||||
02 Q-TMP2 PIC 9(3).
|
||||
02 N PIC 9(4).
|
||||
01 DISPLAYING.
|
||||
02 ITEM PIC Z(3).
|
||||
02 IX PIC Z(4).
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
MAIN-PROGRAM.
|
||||
PERFORM GENERATE-SEQUENCE.
|
||||
PERFORM SHOW-ITEM
|
||||
VARYING N FROM 1 BY 1
|
||||
UNTIL N IS GREATER THAN 10.
|
||||
SET N TO 1000.
|
||||
PERFORM SHOW-ITEM.
|
||||
STOP RUN.
|
||||
|
||||
GENERATE-SEQUENCE.
|
||||
SET Q(1) TO 1.
|
||||
SET Q(2) TO 1.
|
||||
PERFORM GENERATE-ITEM
|
||||
VARYING N FROM 3 BY 1
|
||||
UNTIL N IS GREATER THAN 1000.
|
||||
|
||||
GENERATE-ITEM.
|
||||
COMPUTE Q-TMP1 = N - Q(N - 1).
|
||||
COMPUTE Q-TMP2 = N - Q(N - 2).
|
||||
COMPUTE Q(N) = Q(Q-TMP1) + Q(Q-TMP2).
|
||||
|
||||
SHOW-ITEM.
|
||||
MOVE N TO IX.
|
||||
MOVE Q(N) TO ITEM.
|
||||
DISPLAY 'Q(' IX ') = ' ITEM.
|
||||
22
Task/Hofstadter-Q-sequence/Crystal/hofstadter-q-sequence.cr
Normal file
22
Task/Hofstadter-Q-sequence/Crystal/hofstadter-q-sequence.cr
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class Q
|
||||
@@cache = [0, 1, 1]
|
||||
|
||||
def self.[] (n)
|
||||
if n >= @@cache.size
|
||||
(@@cache.size ... n).each do |i|
|
||||
Q[i]
|
||||
end
|
||||
@@cache << @@cache[n - @@cache[n-1]] + @@cache[n - @@cache[n-2]]
|
||||
end
|
||||
@@cache[n]
|
||||
end
|
||||
end
|
||||
|
||||
print "First 10 terms: "
|
||||
puts (1..10).map {|i| Q[i] }.join(" ")
|
||||
|
||||
puts "1000th term: #{ Q[1000] }"
|
||||
|
||||
c = (2..100_000).count {|i| Q[i] < Q[i-1] }
|
||||
|
||||
puts "Less than preceding: #{c}"
|
||||
18
Task/Hofstadter-Q-sequence/Pluto/hofstadter-q-sequence.pluto
Normal file
18
Task/Hofstadter-Q-sequence/Pluto/hofstadter-q-sequence.pluto
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
require "table2"
|
||||
local fmt = require "fmt"
|
||||
|
||||
local N = 100_000
|
||||
local q = table.rep(N, 0)
|
||||
q[0] = 0
|
||||
q[1] = 1
|
||||
q[2] = 1
|
||||
for n = 3, N do q[n] = q[n - q[n-1]] + q[n - q[n-2]] end
|
||||
|
||||
print("The first ten terms of the Hofstadter Q sequence are:")
|
||||
fmt.lprint(q:slice(1, 10))
|
||||
print($"\nThe thousandth term is {q[1000]}.")
|
||||
local flips = 0
|
||||
for n = 2, N do
|
||||
if (q[n] < q[n-1]) then flips += 1 end
|
||||
end
|
||||
print($"\nThere are {fmt.int(flips)} flips in the first {fmt.int(N)} terms.")
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
Sub q_sequence(n)
|
||||
Dim Q()
|
||||
ReDim Q(n)
|
||||
Q(1)=1 : Q(2)=1 : Q(3)=2
|
||||
less_precede = 0
|
||||
For i = 4 To n
|
||||
Q(i)=Q(i-Q(i-1))+Q(i-Q(i-2))
|
||||
If Q(i) < Q(i-1) Then
|
||||
less_precede = less_precede + 1
|
||||
End If
|
||||
Next
|
||||
WScript.StdOut.Write "First 10 terms of the sequence: "
|
||||
For j = 1 To 10
|
||||
If j < 10 Then
|
||||
WScript.StdOut.Write Q(j) & ", "
|
||||
Else
|
||||
WScript.StdOut.Write "and " & Q(j)
|
||||
End If
|
||||
Next
|
||||
WScript.StdOut.WriteLine
|
||||
WScript.StdOut.Write "1000th term of the sequence: " & Q(1000)
|
||||
WScript.StdOut.WriteLine
|
||||
WScript.StdOut.Write "Number of times the member of the sequence is less than its preceding term: " &_
|
||||
less_precede
|
||||
End Sub
|
||||
|
||||
q_sequence(100000)
|
||||
Loading…
Add table
Add a link
Reference in a new issue