RosettaCodeData/Task/Happy-numbers/Tcl/happy-numbers.tcl
Ingy döt Net db842d013d A-M baby
2013-04-10 21:29:02 -07:00

16 lines
364 B
Tcl

proc is_happy n {
set seen [list]
while {$n > 1 && [lsearch -exact $seen $n] == -1} {
lappend seen $n
set n [sum_of_squares [split $n ""]]
}
return [expr {$n == 1}]
}
set happy [list]
set n -1
while {[llength $happy] < 8} {
if {[is_happy $n]} {lappend happy $n}
incr n
}
puts "the first 8 happy numbers are: [list $happy]"