#!/usr/athena/bin/perl use strict; sub hnext; # get arguments my $n = shift; my $nstop = shift; my $which = lc (shift); # defaults $nstop = $n, $n = 1 if ($nstop == undef); $nstop = 20 if ($nstop == undef); $which = 'Both' if !($which =~ /(happy|sad|both)/); # bounds check $n = 1 if ($n < 1); $nstop = 1 if ($nstop < 1); while ($n <= $nstop) { my $m = $n; my $st = 0; $m = hnext ($m), $st++ while (($m != 1) && (issad ($m) != 1)); if ($m == 1) { print "$n\t$st\n" if ($which =~ /(happy|both)/); print "$n\t$st\t$m\n" if ($which =~ /Both/); } else { print "$n\t$st\n" if ($which =~ /(sad|both)/); print "$n\t$st\t$m\n" if ($which =~ /Both/); } $n++; } exit; sub issad { my @sad = (4, 16, 37, 58, 89, 145, 42, 20); my $n = shift; foreach my $m (@sad) { return 1 if ($m == $n); } return 0; } sub hnext { my $n = shift; my @digits = split (//, $n); my $sum = 0; foreach my $digit (@digits) { $sum = $sum + ($digit * $digit); } return ($sum); }