Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
15
Task/Happy-numbers/Jq/happy-numbers-1.jq
Normal file
15
Task/Happy-numbers/Jq/happy-numbers-1.jq
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
def is_happy_number:
|
||||
def next: tostring | explode | map( (. - 48) | .*.) | add;
|
||||
def last(g): reduce g as $i (null; $i);
|
||||
# state: either 1 or [i, o]
|
||||
# where o is an an object with the previously encountered numbers as keys
|
||||
def loop:
|
||||
recurse( if . == 1 then empty # all done
|
||||
elif .[0] == 1 then 1 # emit 1
|
||||
else (.[0]| next) as $n
|
||||
| if $n == 1 then 1
|
||||
elif .[1]|has($n|tostring) then empty
|
||||
else [$n, (.[1] + {($n|tostring):true}) ]
|
||||
end
|
||||
end );
|
||||
1 == last( [.,{}] | loop );
|
||||
12
Task/Happy-numbers/Jq/happy-numbers-2.jq
Normal file
12
Task/Happy-numbers/Jq/happy-numbers-2.jq
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# Set n to -1 to continue indefinitely:
|
||||
def happy(n):
|
||||
def subtask: # state: [i, found]
|
||||
if .[1] == n then empty
|
||||
else .[0] as $n
|
||||
| if ($n | is_happy_number) then $n, ([ $n+1, .[1]+1 ] | subtask)
|
||||
else (.[0] += 1) | subtask
|
||||
end
|
||||
end;
|
||||
[0,0] | subtask;
|
||||
|
||||
happy($n|tonumber)
|
||||
9
Task/Happy-numbers/Jq/happy-numbers-3.jq
Normal file
9
Task/Happy-numbers/Jq/happy-numbers-3.jq
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
$ jq --arg n 8 -n -f happy.jq
|
||||
1
|
||||
7
|
||||
10
|
||||
13
|
||||
19
|
||||
23
|
||||
28
|
||||
31
|
||||
Loading…
Add table
Add a link
Reference in a new issue