Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Happy-numbers/AWK/happy-numbers-1.awk
Normal file
41
Task/Happy-numbers/AWK/happy-numbers-1.awk
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
function is_happy(n)
|
||||
{
|
||||
if ( n in happy ) return 1;
|
||||
if ( n in unhappy ) return 0;
|
||||
cycle[""] = 0
|
||||
while( (n!=1) && !(n in cycle) ) {
|
||||
cycle[n] = n
|
||||
new_n = 0
|
||||
while(n>0) {
|
||||
d = n % 10
|
||||
new_n += d*d
|
||||
n = int(n/10)
|
||||
}
|
||||
n = new_n
|
||||
}
|
||||
if ( n == 1 ) {
|
||||
for (i_ in cycle) {
|
||||
happy[cycle[i_]] = 1
|
||||
delete cycle[i_]
|
||||
}
|
||||
return 1
|
||||
} else {
|
||||
for (i_ in cycle) {
|
||||
unhappy[cycle[i_]] = 1
|
||||
delete cycle[i_]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
cnt = 0
|
||||
happy[""] = 0
|
||||
unhappy[""] = 0
|
||||
for(j=1; (cnt < 8); j++) {
|
||||
if ( is_happy(j) == 1 ) {
|
||||
cnt++
|
||||
print j
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Task/Happy-numbers/AWK/happy-numbers-2.awk
Normal file
28
Task/Happy-numbers/AWK/happy-numbers-2.awk
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
BEGIN {
|
||||
for (i = 1; i < 50; ++i){
|
||||
if (isHappy(i)) {
|
||||
print i;
|
||||
}
|
||||
}
|
||||
exit
|
||||
}
|
||||
|
||||
function isHappy(n, seen) {
|
||||
delete seen;
|
||||
while (1) {
|
||||
n = sumSqrDig(n)
|
||||
if (seen[n]) {
|
||||
return n == 1
|
||||
}
|
||||
seen[n] = 1
|
||||
}
|
||||
}
|
||||
|
||||
function sumSqrDig(n, d, tot) {
|
||||
while (n) {
|
||||
d = n % 10
|
||||
tot += d * d
|
||||
n = int(n/10)
|
||||
}
|
||||
return tot
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue