Hailstone sequence: Difference between revisions

→‎Tcl: Added implementation (second time lucky)
(New task and Python solution. (second time lucky))
 
(→‎Tcl: Added implementation (second time lucky))
Line 28:
'''Sample Output'''
<pre>Maximum length 351 was found for hailstone(77031) for numbers <100,000</pre>
 
=={{header|Tcl}}==
<lang tcl>proc hailstone n {
while 1 {
lappend seq $n
if {$n == 1} {return $seq}
set n [expr {$n & 1 ? $n*3+1 : $n/2}]
}
}
 
set h27 [hailstone 27]
puts "h27 len=[llength $h27]"
puts "head4 = [lrange $h27 0 3]"
puts "tail4 = [lrange $h27 end-3 end]"
 
set maxlen [set max 0]
for {set i 1} {$i<100000} {incr i} {
set l [llength [hailstone $i]]
if {$l>$maxlen} {set maxlen $l;set max $i}
}
puts "max is $max, with length $maxlen"</lang>
Output:
<pre>
h27 len=112
head4 = 27 82 41 124
tail4 = 8 4 2 1
max is 77031, with length 351
</pre>
Anonymous user