RosettaCodeData/Task/Happy-numbers/PowerShell/happy-numbers-1.psh

21 lines
459 B
Text
Raw Permalink Normal View History

2013-04-10 22:43:41 -07:00
function happy([int] $n) {
$a=@()
for($i=2;$a.count -lt $n;$i++) {
$sum=$i
$hist=@{}
while( $hist[$sum] -eq $null ) {
if($sum -eq 1) {
$a+=$i
}
$hist[$sum]=$sum
$sum2=0
foreach($j in $sum.ToString().ToCharArray()) {
$k=([int]$j)-0x30
$sum2+=$k*$k
}
$sum=$sum2
}
}
2013-10-27 22:24:23 +00:00
$a -join ','
2013-04-10 22:43:41 -07:00
}