March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
18
Task/Happy-numbers/AutoHotkey/happy-numbers-2.ahk
Normal file
18
Task/Happy-numbers/AutoHotkey/happy-numbers-2.ahk
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
while h < 8
|
||||
if (Happy(A_Index)) {
|
||||
Out .= A_Index A_Space
|
||||
h++
|
||||
}
|
||||
MsgBox, % Out
|
||||
|
||||
Happy(n) {
|
||||
Loop, {
|
||||
Loop, Parse, n
|
||||
t += A_LoopField ** 2
|
||||
if (t = 89)
|
||||
return, 0
|
||||
if (t = 1)
|
||||
return, 1
|
||||
n := t, t := 0
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ bool isHappy(int n) pure /*nothrow*/ {
|
|||
int[int] seen;
|
||||
|
||||
while (true) {
|
||||
immutable t = n.text.map!q{(a - '0') ^^ 2}.reduce!q{a + b};
|
||||
immutable t = n.text.map!q{(a - '0') ^^ 2}.sum;
|
||||
if (t == 1)
|
||||
return true;
|
||||
if (t in seen)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
>>> def happy(n):
|
||||
past = set()
|
||||
while n <> 1:
|
||||
while n != 1:
|
||||
n = sum(int(i)**2 for i in str(n))
|
||||
if n in past:
|
||||
return False
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ haps=0 /*count of happy numbers so far. */
|
|||
s=s+#._ /*add the square of a digit. */
|
||||
end /*j*/
|
||||
|
||||
if @.s then do; q=1; iterate; end /*we have found a happy number.*/
|
||||
if @.s then leave /*we have found a happy number.*/
|
||||
if !.s then iterate n /*Sum unhappy? Then Q is unhappy*/
|
||||
if a.s then do /*If already summed? Q is unhappy*/
|
||||
!.q=1; !.s=1 /*mark Q & S as unhappy numbers*/
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ sw=linesize() /*obtain the linesize of term scr*/
|
|||
s=s+#._ /*add the square of a digit. */
|
||||
end /*j*/
|
||||
|
||||
if @.s then do; q=1; iterate; end /*we have found a happy number.*/
|
||||
if @.s then leave /*we have found a happy number.*/
|
||||
if !.s then iterate n /*Sum unhappy? Then Q is unhappy*/
|
||||
if a.s then do /*If already summed? Q is unhappy*/
|
||||
!.q=1; !.s=1 /*mark Q & S as unhappy numbers*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue