Happy numbers: Difference between revisions

→‎{{header|UNIX Shell}}: Make solution compatible with ksh and zsh.
m (syntax highlighting fixup automation)
(→‎{{header|UNIX Shell}}: Make solution compatible with ksh and zsh.)
Line 6,713:
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
<syntaxhighlight lang="bash">#!/bin/bash
{{works with|Z Shell}}
function sum_of_square_digits
<syntaxhighlight lang="bash">#!/bin/bashfunction sum_of_square_digits {
{
localtypeset -i n="$1" sum=0 d
while (( n )); do
local -i(( d=n%10, sum+=d*d, n=n/10 ))
let sum+=d*d
let n=n/10
done
echoprintf '%d\n' "$sum"
}
 
function is_happy? {
typeset -i n=$1
{
localtypeset -ia nseen="$1"()
local seen=()
while (( n != 1 )); do
if [[ -n "${seen[$n]}" ]]; then
return 1
fi
seen[$n]=1
let(( n="$(sum_of_square_digits "$n")" ))
done
return 0
}
 
function first_n_happy {
typeset let-i count-=$1 n
{
localfor -i(( n=1; count; n+="$1" )); do
if is_happy? "$n"; then
local -i n
printf '%d\n' "$n"
for (( n=0; count; n+-= 1 )); do
if is_happy? "$n"; then
echo "$n"fi
let count-=1
fi
done
return 0
}
 
first_n_happy 8</syntaxhighlightpre>
Output:<pre>1
7
10
13
19
23
28
31</pre>
 
=={{header|Ursala}}==
1,480

edits