Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Hofstadter-Q-sequence/Tcl/hofstadter-q-sequence.tcl
Normal file
24
Task/Hofstadter-Q-sequence/Tcl/hofstadter-q-sequence.tcl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package require Tcl 8.5
|
||||
|
||||
# Index 0 is not used, but putting it in makes the code a bit shorter
|
||||
set tcl::mathfunc::Qcache {Q:-> 1 1}
|
||||
proc tcl::mathfunc::Q {n} {
|
||||
variable Qcache
|
||||
if {$n >= [llength $Qcache]} {
|
||||
lappend Qcache [expr {Q($n - Q($n-1)) + Q($n - Q($n-2))}]
|
||||
}
|
||||
return [lindex $Qcache $n]
|
||||
}
|
||||
|
||||
# Demonstration code
|
||||
for {set i 1} {$i <= 10} {incr i} {
|
||||
puts "Q($i) == [expr {Q($i)}]"
|
||||
}
|
||||
# This runs very close to recursion limit...
|
||||
puts "Q(1000) == [expr Q(1000)]"
|
||||
# This code is OK, because the calculations are done step by step
|
||||
set q [expr Q(1)]
|
||||
for {set i 2} {$i <= 100000} {incr i} {
|
||||
incr count [expr {$q > [set q [expr {Q($i)}]]}]
|
||||
}
|
||||
puts "Q(i)<Q(i-1) for i \[2..100000\] is true $count times"
|
||||
Loading…
Add table
Add a link
Reference in a new issue