2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -5,8 +5,12 @@ From Wikipedia, the free encyclopedia:
|
|||
|
||||
Display an example of your output here.
|
||||
|
||||
'''Task:''' Find and print the first 8 happy numbers.
|
||||
|
||||
See also:
|
||||
;task:
|
||||
Find and print the first 8 happy numbers.
|
||||
|
||||
|
||||
;See also
|
||||
* [[oeis:A007770|The happy numbers on OEIS: A007770]]
|
||||
* [[oeis:A031177|The unhappy numbers on OEIS; A031177]]
|
||||
<br><br>
|
||||
|
|
|
|||
32
Task/Happy-numbers/AppleScript/happy-numbers-1.applescript
Normal file
32
Task/Happy-numbers/AppleScript/happy-numbers-1.applescript
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
on run
|
||||
set howManyHappyNumbers to 8
|
||||
set happyNumberList to {}
|
||||
set globalCounter to 1
|
||||
|
||||
repeat howManyHappyNumbers times
|
||||
repeat while not isHappy(globalCounter)
|
||||
set globalCounter to globalCounter + 1
|
||||
end repeat
|
||||
set end of happyNumberList to globalCounter
|
||||
set globalCounter to globalCounter + 1
|
||||
end repeat
|
||||
log happyNumberList
|
||||
end run
|
||||
|
||||
on isHappy(numberToCheck)
|
||||
set localCycle to {}
|
||||
repeat while (numberToCheck ≠ 1)
|
||||
if localCycle contains numberToCheck then
|
||||
exit repeat
|
||||
end if
|
||||
set end of localCycle to numberToCheck
|
||||
set tempNumber to 0
|
||||
repeat while (numberToCheck > 0)
|
||||
set digitOfNumber to numberToCheck mod 10
|
||||
set tempNumber to tempNumber + (digitOfNumber ^ 2)
|
||||
set numberToCheck to (numberToCheck - digitOfNumber) / 10
|
||||
end repeat
|
||||
set numberToCheck to tempNumber
|
||||
end repeat
|
||||
return (numberToCheck = 1)
|
||||
end isHappy
|
||||
131
Task/Happy-numbers/AppleScript/happy-numbers-2.applescript
Normal file
131
Task/Happy-numbers/AppleScript/happy-numbers-2.applescript
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
-- isHappy :: Int -> Bool
|
||||
on isHappy(n)
|
||||
|
||||
-- endsInOne :: [Int] -> Int -> Bool
|
||||
script endsInOne
|
||||
|
||||
-- sumOfSquaredDigits :: Int -> Int
|
||||
script sumOfSquaredDigits
|
||||
|
||||
-- digitSquared :: Int -> Int -> Int
|
||||
script digitSquared
|
||||
on lambda(a, x)
|
||||
(a + (x as integer) ^ 2) as integer
|
||||
end lambda
|
||||
end script
|
||||
|
||||
on lambda(n)
|
||||
foldl(digitSquared, 0, splitOn("", n as string))
|
||||
end lambda
|
||||
end script
|
||||
|
||||
-- [Int] -> Int -> Bool
|
||||
on lambda(s, n)
|
||||
if n = 1 then
|
||||
true
|
||||
else
|
||||
if s contains n then
|
||||
false
|
||||
else
|
||||
lambda(s & n, lambda(n) of sumOfSquaredDigits)
|
||||
end if
|
||||
end if
|
||||
end lambda
|
||||
end script
|
||||
|
||||
endsInOne's lambda({}, n)
|
||||
end isHappy
|
||||
|
||||
|
||||
-- TEST
|
||||
on run
|
||||
|
||||
-- seriesLength :: {n:Int, xs:[Int]} -> Bool
|
||||
script seriesLength
|
||||
property target : 8
|
||||
|
||||
on lambda(rec)
|
||||
length of xs of rec = target of seriesLength
|
||||
end lambda
|
||||
end script
|
||||
|
||||
-- succTest :: {n:Int, xs:[Int]} -> {n:Int, xs:[Int]}
|
||||
script succTest
|
||||
on lambda(rec)
|
||||
set xs to xs of rec
|
||||
set n to n of rec
|
||||
|
||||
script testResult
|
||||
on lambda(x)
|
||||
if isHappy(x) then
|
||||
xs & x
|
||||
else
|
||||
xs
|
||||
end if
|
||||
end lambda
|
||||
end script
|
||||
|
||||
{n:n + 1, xs:testResult's lambda(n)}
|
||||
end lambda
|
||||
end script
|
||||
|
||||
xs of |until|(seriesLength, succTest, {n:1, xs:{}})
|
||||
|
||||
--> {1, 7, 10, 13, 19, 23, 28, 31}
|
||||
end run
|
||||
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to lambda(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
-- until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
on |until|(p, f, x)
|
||||
set mp to mReturn(p)
|
||||
set mf to mReturn(f)
|
||||
|
||||
script
|
||||
property p : mp's lambda
|
||||
property f : mf's lambda
|
||||
|
||||
on lambda(v)
|
||||
repeat until p(v)
|
||||
set v to f(v)
|
||||
end repeat
|
||||
return v
|
||||
end lambda
|
||||
end script
|
||||
|
||||
result's lambda(x)
|
||||
end |until|
|
||||
|
||||
-- splitOn :: Text -> Text -> [Text]
|
||||
on splitOn(strDelim, strMain)
|
||||
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
|
||||
set xs to text items of strMain
|
||||
set my text item delimiters to dlm
|
||||
return xs
|
||||
end splitOn
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -0,0 +1 @@
|
|||
{1, 7, 10, 13, 19, 23, 28, 31}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
on run
|
||||
set howManyHappyNumbers to 8
|
||||
set happyNumberList to {}
|
||||
set globalCounter to 1
|
||||
|
||||
repeat howManyHappyNumbers times
|
||||
repeat while not isHappy(globalCounter)
|
||||
set globalCounter to globalCounter + 1
|
||||
end repeat
|
||||
set end of happyNumberList to globalCounter
|
||||
set globalCounter to globalCounter + 1
|
||||
end repeat
|
||||
log happyNumberList
|
||||
end run
|
||||
|
||||
on isHappy(numberToCheck)
|
||||
set localCycle to {}
|
||||
repeat while (numberToCheck ≠ 1)
|
||||
if localCycle contains numberToCheck then
|
||||
exit repeat
|
||||
end if
|
||||
set end of localCycle to numberToCheck
|
||||
set tempNumber to 0
|
||||
repeat while (numberToCheck > 0)
|
||||
set digitOfNumber to numberToCheck mod 10
|
||||
set tempNumber to tempNumber + (digitOfNumber ^ 2)
|
||||
set numberToCheck to (numberToCheck - digitOfNumber) / 10
|
||||
end repeat
|
||||
set numberToCheck to tempNumber
|
||||
end repeat
|
||||
return (numberToCheck = 1)
|
||||
end isHappy
|
||||
25
Task/Happy-numbers/JavaScript/happy-numbers-2.js
Normal file
25
Task/Happy-numbers/JavaScript/happy-numbers-2.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// isHappy :: Int -> Bool
|
||||
function isHappy(n) {
|
||||
let f = n => n.toString()
|
||||
.split('')
|
||||
.reduce((a, x) => a + Math.pow(parseInt(x, 10), 2), 0),
|
||||
p = (s, n) => n === 1 ? true : (
|
||||
s.has(n) ? false : p(s.add(n), f(n))
|
||||
);
|
||||
return p(new Set(), n);
|
||||
}
|
||||
|
||||
// TEST
|
||||
|
||||
// range :: Int -> Int -> [Int]
|
||||
let range = (m, n) => Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
return range(1, 50)
|
||||
.filter(isHappy)
|
||||
.slice(0, 8);
|
||||
})()
|
||||
1
Task/Happy-numbers/JavaScript/happy-numbers-3.js
Normal file
1
Task/Happy-numbers/JavaScript/happy-numbers-3.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
[1, 7, 10, 13, 19, 23, 28, 31]
|
||||
35
Task/Happy-numbers/JavaScript/happy-numbers-4.js
Normal file
35
Task/Happy-numbers/JavaScript/happy-numbers-4.js
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// isHappy :: Int -> Bool
|
||||
let isHappy = n => {
|
||||
let f = n => n.toString()
|
||||
.split('')
|
||||
.reduce((a, x) => a + Math.pow(parseInt(x, 10), 2), 0),
|
||||
p = (s, n) => n === 1 ? true : (
|
||||
s.has(n) ? false : p(s.add(n), f(n))
|
||||
);
|
||||
return p(new Set(), n);
|
||||
},
|
||||
|
||||
// until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
until = (p, f, x) => {
|
||||
let v = x;
|
||||
while (!p(v)) v = f(v);
|
||||
return v;
|
||||
};
|
||||
|
||||
return until(
|
||||
m => m.xs.length === 8,
|
||||
m => {
|
||||
let n = m.n;
|
||||
return {
|
||||
n: n + 1,
|
||||
xs: isHappy(n) ? m.xs.concat(n) : m.xs
|
||||
};
|
||||
}, {
|
||||
n: 1,
|
||||
xs: []
|
||||
}
|
||||
).xs;
|
||||
})();
|
||||
1
Task/Happy-numbers/JavaScript/happy-numbers-5.js
Normal file
1
Task/Happy-numbers/JavaScript/happy-numbers-5.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
[1, 7, 10, 13, 19, 23, 28, 31]
|
||||
|
|
@ -1,19 +1,19 @@
|
|||
/*REXX program computes and displays a specified number of happy numbers. */
|
||||
parse arg limit . /*get optional arguments from the C.L. */
|
||||
if limit=='' | limit==',' then limit=8 /*Not specified? Then use the default.*/
|
||||
haps=0 /*count of the happy numbers (so far).*/
|
||||
/*REXX program computes and displays a specified amount of happy numbers. */
|
||||
parse arg limit . /*obtain optional argument from the CL.*/
|
||||
if limit=='' | limit=="," then limit=8 /*Not specified? Then use the default.*/
|
||||
haps=0 /*count of the happy numbers (so far).*/
|
||||
|
||||
do n=1 while haps<limit; @.=0 /*search the integers starting at unity*/
|
||||
q=n; do until q==1 /*determine if Q is a happy number.*/
|
||||
s=0 /*prepare to add squares of digits. */
|
||||
do j=1 for length(q) /*sum the squares of the decimal digits*/
|
||||
s=s+substr(q,j,1)**2 /*add the square of a decimal digit.*/
|
||||
do n=1 while haps<limit; @.=0; q=n /*search the integers starting at unity*/
|
||||
do until q==1 /*determine if Q is a happy number.*/
|
||||
s=0 /*prepare to add squares of digits. */
|
||||
do j=1 for length(q) /*sum the squares of the decimal digits*/
|
||||
s=s + substr(q, j, 1) **2 /*add the square of a decimal digit.*/
|
||||
end /*j*/
|
||||
|
||||
if @.s then iterate n /*if already summed, Q is unhappy. */
|
||||
@.s=1; q=s /*mark the sum as found; try Q sum.*/
|
||||
if @.s then iterate n /*if already summed, Q is unhappy. */
|
||||
@.s=1; q=s /*mark the sum as found; try Q sum.*/
|
||||
end /*until*/
|
||||
say n /*display the number (N is happy). */
|
||||
haps=haps+1 /*bump the count of happy numbers. */
|
||||
say n /*display the number (N is happy). */
|
||||
haps=haps+1 /*bump the count of happy numbers. */
|
||||
end /*n*/
|
||||
/*stick a fork in it, we're all done. */
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -1,27 +1,27 @@
|
|||
/*REXX program computes and displays a specified range of happy numbers. */
|
||||
parse arg L H . /*get optional arguments from the C.L. */
|
||||
if L=='' | L==',' then L=8 /*Not specified? Then use the default.*/
|
||||
if H=='' | H==',' then do;H=L;L=1; end /*use a range for the displaying of #s.*/
|
||||
do i=0 to 9; #.i=i**2; end /*i*/ /*build a squared decimal digit table. */
|
||||
@.=0; @.1=1; !.=@.; !.2=1; !.4=1 /*sparse array: @≡happy, !≡unhappy. */
|
||||
haps=0 /*count of the happy numbers (so far).*/
|
||||
/*REXX program computes and displays a specified range of happy numbers. */
|
||||
parse arg L H . /*obtain optional arguments from the CL*/
|
||||
if L=='' | L=="," then L=8 /*Not specified? Then use the default.*/
|
||||
if H=='' | H=="," then do; H=L; L=1; end /*use a range for the displaying of #s.*/
|
||||
do i=0 to 9; #.i=i**2; end /*i*/ /*build a squared decimal digit table. */
|
||||
@.=0; @.1=1; !.=@.; !.2=1; !.4=1 /*sparse array: @≡happy, !≡unhappy. */
|
||||
haps=0 /*count of the happy numbers (so far).*/
|
||||
|
||||
do n=1 while haps<H /*search integers starting at unity. */
|
||||
if !.n then iterate /*if N is unhappy, then try another. */
|
||||
q=n /* [↓] Q is the number being tested*/
|
||||
do until q==1; s=0 /*see if Q is a happy number. */
|
||||
?=q /* [↓] ? is destructively PARSEd. */
|
||||
do length(q) /*parse all the decimal digits of ? */
|
||||
parse var ? _ +1 ? /*obtain a single decimal digit of ? */
|
||||
s=s + #._ /*add the square of that decimal digit.*/
|
||||
end /*length(q)*/ /* [↑] perform the DO W times. */
|
||||
if !.s then do; !.n=1; iterate n; end /*S unhappy? Then Q also.*/
|
||||
if @.s then leave /*Have we found a happy number? */
|
||||
q=s /*try the Q sum to see if it's happy.*/
|
||||
end /*until*/
|
||||
@.n=1 /*mark N as a happy number.*/
|
||||
haps=haps+1 /*bump the counter of the happy numbers*/
|
||||
if haps<L then iterate /*don't display if N is too low.*/
|
||||
say right(n, 30) /*display right justified happy number.*/
|
||||
end /*n*/
|
||||
/*stick a fork in it, we're all done. */
|
||||
do n=1 while haps<H /*search integers starting at unity. */
|
||||
if !.n then iterate /*if N is unhappy, then try another. */
|
||||
q=n /* [↓] Q is the number being tested*/
|
||||
do until q==1; s=0 /*see if Q is a happy number. */
|
||||
?=q /* [↓] ? is destructively parsed. */
|
||||
do length(q) /*parse all the decimal digits of ? */
|
||||
parse var ? _ +1 ? /*obtain a single decimal digit of ? */
|
||||
s=s + #._ /*add the square of that decimal digit.*/
|
||||
end /*length(q)*/ /* [↑] perform the DO W times. */
|
||||
if !.s then do; !.n=1; iterate n; end /*is S unhappy? Then Q is also. */
|
||||
if @.s then leave /*Have we found a happy number? */
|
||||
q=s /*try the Q sum to see if it's happy.*/
|
||||
end /*until*/
|
||||
@.n=1 /*mark N as a happy number.*/
|
||||
haps=haps+1 /*bump the counter of the happy numbers*/
|
||||
if haps<L then iterate /*don't display if N is too low.*/
|
||||
say right(n, 30) /*display right justified happy number.*/
|
||||
end /*n*/
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -1,34 +1,34 @@
|
|||
/*REXX program computes and displays a specified range of happy numbers. */
|
||||
sw=linesize() /*obtain the screen width of terminal. */
|
||||
parse arg L H . /*get optional arguments from the C.L. */
|
||||
if L=='' | L==',' then L=8 /*Not specified? Then use the default.*/
|
||||
if H=='' | H==',' then do;H=L;L=1; end /*use a range for the displaying of #s.*/
|
||||
do i=0 to 9; #.i=i**2; end /*i*/ /*build a squared decimal digit table. */
|
||||
@.=0; @.1=1; !.=@.; !.2=1; !.4=1 /*sparse array: @≡happy, !≡unhappy. */
|
||||
haps=0 /*count of the happy numbers (so far).*/
|
||||
/*REXX program computes and displays a specified range of happy numbers. */
|
||||
sw=linesize() - 1 /*obtain the screen width (less one). */
|
||||
parse arg limit . /*obtain optional argument from the CL.*/
|
||||
if L=='' | L=="," then L=8 /*Not specified? Then use the default.*/
|
||||
if H=='' | H=="," then do; H=L; L=1; end /*use a range for the displaying of #s.*/
|
||||
do i=0 to 9; #.i=i**2; end /*i*/ /*build a squared decimal digit table. */
|
||||
@.=0; @.1=1; !.=@.; !.2=1; !.4=1 /*sparse array: @≡happy, !≡unhappy. */
|
||||
haps=0 /*count of the happy numbers (so far).*/
|
||||
$=
|
||||
do n=1 while haps<H /*search integers starting at unity. */
|
||||
if !.n then iterate /*if N is unhappy, then try another. */
|
||||
q=n /*(below) Q is the number tested. */
|
||||
do until q==1; s=0 /*see if Q is a happy number. */
|
||||
?=q /* [↓] ? is destructively PARSEd. */
|
||||
do length(q) /*parse all the decimal digits of ? */
|
||||
parse var ? _ +1 ? /*obtain a single decimal digit of ? */
|
||||
s=s + #._ /*add the square of that decimal digit.*/
|
||||
end /*length(q)*/ /* [↑] perform the DO W times. */
|
||||
do n=1 while haps<H /*search integers starting at unity. */
|
||||
if !.n then iterate /*if N is unhappy, then try another. */
|
||||
q=n /*(below) Q is the number tested. */
|
||||
do until q==1; s=0 /*see if Q is a happy number. */
|
||||
?=q /* [↓] ? is destructively PARSEd. */
|
||||
do length(q) /*parse all the decimal digits of ? */
|
||||
parse var ? _ +1 ? /*obtain a single decimal digit of ? */
|
||||
s=s + #._ /*add the square of that decimal digit.*/
|
||||
end /*length(q)*/ /* [↑] perform the DO W times. */
|
||||
|
||||
if !.s then do; !.n=1; iterate n; end /*S unhappy? Then Q also.*/
|
||||
if @.s then leave /*Have we found a happy number? */
|
||||
q=s /*try the Q sum to see if it's happy.*/
|
||||
end /*until*/
|
||||
@.n=1 /*mark N as a happy number. */
|
||||
haps=haps+1 /*bump the count of the happy numbers. */
|
||||
if haps<L then iterate /*don't display it, N is too low. */
|
||||
$=$ n /*add N to the horizontal list. */
|
||||
if length($ n)>sw then do /*if the list is too long, then split */
|
||||
say strip($) /*··· and display what we've got. */
|
||||
$=n /*Set the next line to overflow. */
|
||||
end /* [↑] now contains overflow. */
|
||||
if !.s then do; !.n=1; iterate n; end /*is S unhappy? Then Q is also. */
|
||||
if @.s then leave /*Have we found a happy number? */
|
||||
q=s /*try the Q sum to see if it's happy.*/
|
||||
end /*until*/
|
||||
@.n=1 /*mark N as a happy number. */
|
||||
haps=haps+1 /*bump the count of the happy numbers. */
|
||||
if haps<L then iterate /*don't display it, N is too low. */
|
||||
$=$ n /*add N to the horizontal list. */
|
||||
if length($ n)>sw then do /*if the list is too long, then split */
|
||||
say strip($) /* ··· and display what we've got. */
|
||||
$=n /*Set the next line to overflow. */
|
||||
end /* [↑] new line now contains overflow.*/
|
||||
end /*n*/
|
||||
if $\='' then say strip($) /*display any residual happy numbers. */
|
||||
/*stick a fork in it, we're all done. */
|
||||
if $\='' then say strip($) /*display any residual happy numbers. */
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
16
Task/Happy-numbers/ZX-Spectrum-Basic/happy-numbers.zx
Normal file
16
Task/Happy-numbers/ZX-Spectrum-Basic/happy-numbers.zx
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
10 FOR i=1 TO 100
|
||||
20 GO SUB 1000
|
||||
30 IF isHappy=1 THEN PRINT i;" is a happy number"
|
||||
40 NEXT i
|
||||
50 STOP
|
||||
1000 REM Is Happy?
|
||||
1010 LET isHappy=0: LET count=0: LET num=i
|
||||
1020 IF count=50 OR isHappy=1 THEN RETURN
|
||||
1030 LET n$=STR$ (num)
|
||||
1040 LET count=count+1
|
||||
1050 LET isHappy=0
|
||||
1060 FOR j=1 TO LEN n$
|
||||
1070 LET isHappy=isHappy+VAL n$(j)^2
|
||||
1080 NEXT j
|
||||
1090 LET num=isHappy
|
||||
1100 GO TO 1020
|
||||
Loading…
Add table
Add a link
Reference in a new issue