Happy numbers: Difference between revisions

php5 code added
(php5 code added)
Line 616:
++$happy == 8 and last;
}</lang>
 
=={{header|PHP5}}==
<lang php>
class happynumber
{
/*
For example, 7 is happy, as the associated sequence is:
7² = 4
4² + 9² = 97
9² + 7² = 130
1² + 3² + 0² = 10
1² + 0² = 1.
*/
 
var $list,$num;
static $cache;
 
function __construct()
{
$this->list=array();
}
static function run($anz,$beg=1)
{
for($i=0,$x=$beg;$i < $anz;$i++)
{
while(!$z=happynumber::num($x)) $x++;
$x++;
echo "\n".$z;
}
}
 
function ishappy($num)
{
$sum=0;
$this->list[]=$num;
foreach(str_split($num) as $v)
{
$sum+=($v*$v);
}
###
if($sum==1)return true;
elseif(in_array($sum,$this->list))return false;
else return $this->ishappy($sum);
}
 
static function num($p)
{
$obj=new happynumber();
$out= ($obj->ishappy($p))? $p : false;
return $out;
}
}
###
for($a=1;$a<10;$a++) happynumber::run(10,$a*10000);
 
</lang>
 
=={{header|PicoLisp}}==