Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Happy-numbers/APL/happy-numbers-1.apl
Normal file
20
Task/Happy-numbers/APL/happy-numbers-1.apl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
∇ HappyNumbers arg;⎕IO;∆roof;∆first;bin;iroof
|
||||
[1] ⍝0: Happy number
|
||||
[2] ⍝1: http://rosettacode.org/wiki/Happy_numbers
|
||||
[3] ⎕IO←1 ⍝ Index origin
|
||||
[4] ∆roof ∆first←2↑arg,10 ⍝
|
||||
[5]
|
||||
[6] bin←{
|
||||
[7] ⍺←⍬ ⍝ Default left arg
|
||||
[8] ⍵=1:1 ⍝ Always happy!
|
||||
[9]
|
||||
[10] numbers←⍎¨1⊂⍕⍵ ⍝ Split numbers into parts
|
||||
[11] next←+/{⍵*2}¨numbers ⍝ Sum and square of numbers
|
||||
[12]
|
||||
[13] next∊⍺:0 ⍝ Return 0, if already exists
|
||||
[14] (⍺,next)∇ next ⍝ Check next number (recursive)
|
||||
[15]
|
||||
[16] }¨iroof←⍳∆roof ⍝ Does all numbers upto ∆root smiles?
|
||||
[17]
|
||||
[18] ⎕←~∘0¨∆first↑bin/iroof ⍝ Show ∆first numbers, but not 0
|
||||
∇
|
||||
14
Task/Happy-numbers/APL/happy-numbers-2.apl
Normal file
14
Task/Happy-numbers/APL/happy-numbers-2.apl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
HappyNumbers←{ ⍝ return the first ⍵ Happy Numbers
|
||||
⍺←⍬ ⍝ initial list
|
||||
⍵=+/⍺:⍸⍺ ⍝ 1's mark happy numbers
|
||||
sq←×⍨ ⍝ square function (times selfie)
|
||||
isHappy←{ ⍝ is ⍵ a happy number?
|
||||
⍺←⍬ ⍝ previous sums
|
||||
⍵=1:1 ⍝ if we get to 1, it's happy
|
||||
n←+/sq∘⍎¨⍕⍵ ⍝ sum of the square of the digits
|
||||
n∊⍺:0 ⍝ if we hit this sum before, it's not happy
|
||||
(⍺,n)∇ n} ⍝ recurse until it's happy or not
|
||||
(⍺,isHappy 1+≢⍺)∇ ⍵ ⍝ recurse until we have ⍵ happy numbers
|
||||
}
|
||||
HappyNumbers 8
|
||||
1 7 10 13 19 23 28 31
|
||||
Loading…
Add table
Add a link
Reference in a new issue