Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,13 +1,14 @@
|
|||
(defn- digit-to-num [d] (Character/digit d 10))
|
||||
(defn- square [n] (* n n))
|
||||
|
||||
(defn happy? [n]
|
||||
(loop [n n, seen #{}]
|
||||
(cond (= n 1) true
|
||||
(seen n) false
|
||||
:else
|
||||
(recur (reduce + (map (comp square digit-to-num) (str n)))
|
||||
(conj seen n)))))
|
||||
(cond
|
||||
(= n 1) true
|
||||
(seen n) false
|
||||
:else
|
||||
(recur (->> (str n)
|
||||
(map #(Character/digit % 10))
|
||||
(map #(* % %))
|
||||
(reduce +))
|
||||
(conj seen n)))))
|
||||
|
||||
(def happy-numbers (filter happy? (iterate inc 1)))
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
bool isHappy(int n) pure nothrow {
|
||||
int[int] past;
|
||||
|
||||
|
|
@ -19,5 +17,7 @@ bool isHappy(int n) pure nothrow {
|
|||
}
|
||||
|
||||
void main() {
|
||||
int.max.iota().filter!isHappy().take(8).writeln();
|
||||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
int.max.iota.filter!isHappy.take(8).writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
import std.stdio, std.algorithm, std.range, std.conv;
|
||||
import std.stdio, std.algorithm, std.range, std.conv, std.string;
|
||||
|
||||
bool isHappy(int n) pure /*nothrow*/ {
|
||||
bool isHappy(int n) pure nothrow {
|
||||
int[int] seen;
|
||||
|
||||
while (true) {
|
||||
immutable t = n.text.map!q{(a - '0') ^^ 2}.sum;
|
||||
immutable t = n.text.representation.map!q{(a - '0') ^^ 2}.sum;
|
||||
if (t == 1)
|
||||
return true;
|
||||
if (t in seen)
|
||||
|
|
|
|||
70
Task/Happy-numbers/Eiffel/happy-numbers.e
Normal file
70
Task/Happy-numbers/Eiffel/happy-numbers.e
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
-- Run application.
|
||||
local
|
||||
l_val: INTEGER
|
||||
do
|
||||
from
|
||||
l_val := 1
|
||||
until
|
||||
l_val > 100
|
||||
loop
|
||||
if is_happy_number (l_val) then
|
||||
print (l_val.out)
|
||||
print ("%N")
|
||||
end
|
||||
l_val := l_val + 1
|
||||
end
|
||||
end
|
||||
|
||||
feature -- Happy number
|
||||
|
||||
is_happy_number (a_number: INTEGER): BOOLEAN
|
||||
-- Is `a_number' a happy number?
|
||||
require
|
||||
positive_number: a_number > 0
|
||||
local
|
||||
l_number: INTEGER
|
||||
l_set: ARRAYED_SET [INTEGER]
|
||||
do
|
||||
from
|
||||
l_number := a_number
|
||||
create l_set.make (10)
|
||||
until
|
||||
l_number = 1 or l_set.has (l_number)
|
||||
loop
|
||||
l_set.put (l_number)
|
||||
l_number := square_sum_of_digits (l_number)
|
||||
end
|
||||
|
||||
Result := (l_number = 1)
|
||||
end
|
||||
|
||||
feature{NONE} -- Implementation
|
||||
|
||||
square_sum_of_digits (a_number: INTEGER): INTEGER
|
||||
-- Sum of the sqares of digits of `a_number'.
|
||||
require
|
||||
positive_number: a_number > 0
|
||||
local
|
||||
l_number, l_digit: INTEGER
|
||||
do
|
||||
from
|
||||
l_number := a_number
|
||||
until
|
||||
l_number = 0
|
||||
loop
|
||||
l_digit := l_number \\ 10
|
||||
Result := Result + l_digit * l_digit
|
||||
l_number := l_number // 10
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -2,16 +2,16 @@ module Happy where
|
|||
|
||||
import Prelude.Math
|
||||
-- ugh, since Frege doesn't have Set, use Map instead
|
||||
import Data.Map (member, insertMin, empty)
|
||||
import Data.Map (member, insertMin, empty emptyMap)
|
||||
|
||||
digitToInteger :: Char -> Integer
|
||||
digitToInteger c = fromInt $ (ord c) - (ord '0')
|
||||
|
||||
isHappy :: Integer -> Bool
|
||||
isHappy = p empty
|
||||
isHappy = p emptyMap
|
||||
where p _ 1n = true
|
||||
p s n | n `member` s = false
|
||||
| otherwise = p (insertMin n () s) (f n)
|
||||
f = sum . map (sqr . digitToInteger) . unpacked . show
|
||||
|
||||
main _ = printStrLn $ unwords $ map show $ take 8 $ filter isHappy $ iterate (+ 1n) 1n
|
||||
main _ = putStrLn $ unwords $ map show $ take 8 $ filter isHappy $ iterate (+ 1n) 1n
|
||||
|
|
|
|||
|
|
@ -1,33 +1,29 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
import "fmt"
|
||||
|
||||
func happy(n int) bool {
|
||||
m := make(map[int]int)
|
||||
for n > 1 {
|
||||
m[n] = 0
|
||||
s := strconv.Itoa(n)
|
||||
n = 0
|
||||
for _, d := range s {
|
||||
x := int(d) - '0'
|
||||
n += x * x
|
||||
}
|
||||
if _, ok := m[n]; ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
m := make(map[int]bool)
|
||||
for n > 1 {
|
||||
m[n] = true
|
||||
var x int
|
||||
for x, n = n, 0; x > 0; x /= 10 {
|
||||
d := x % 10
|
||||
n += d * d
|
||||
}
|
||||
if m[n] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func main() {
|
||||
for found, n := 0, 1; found < 8; n++ {
|
||||
if happy(n) {
|
||||
fmt.Print(n, " ")
|
||||
found++
|
||||
}
|
||||
}
|
||||
fmt.Println("")
|
||||
for found, n := 0, 1; found < 8; n++ {
|
||||
if happy(n) {
|
||||
fmt.Print(n, " ")
|
||||
found++
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
|
|
|||
15
Task/Happy-numbers/Groovy/happy-numbers.groovy
Normal file
15
Task/Happy-numbers/Groovy/happy-numbers.groovy
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
Number.metaClass.isHappy = {
|
||||
def number = delegate as Long
|
||||
def cycle = new HashSet<Long>()
|
||||
while (number != 1 && !cycle.contains(number)) {
|
||||
cycle << number
|
||||
number = (number as String).collect { d = (it as Long); d * d }.sum()
|
||||
}
|
||||
number == 1
|
||||
}
|
||||
|
||||
def matches = []
|
||||
for (int i = 0; matches.size() < 8; i++) {
|
||||
if (i.happy) { matches << i }
|
||||
}
|
||||
println matches
|
||||
22
Task/Happy-numbers/MATLAB/happy-numbers.m
Normal file
22
Task/Happy-numbers/MATLAB/happy-numbers.m
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
function findHappyNumbers
|
||||
nHappy = 0;
|
||||
k = 1;
|
||||
while nHappy < 8
|
||||
if isHappyNumber(k, [])
|
||||
fprintf('%d ', k)
|
||||
nHappy = nHappy+1;
|
||||
end
|
||||
k = k+1;
|
||||
end
|
||||
fprintf('\n')
|
||||
end
|
||||
|
||||
function hap = isHappyNumber(k, prev)
|
||||
if k == 1
|
||||
hap = true;
|
||||
elseif ismember(k, prev)
|
||||
hap = false;
|
||||
else
|
||||
hap = isHappyNumber(sum((sprintf('%d', k)-'0').^2), [prev k]);
|
||||
end
|
||||
end
|
||||
26
Task/Happy-numbers/MAXScript/happy-numbers-1.max
Normal file
26
Task/Happy-numbers/MAXScript/happy-numbers-1.max
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
fn isHappyNumber n =
|
||||
(
|
||||
local pastNumbers = #()
|
||||
while n != 1 do
|
||||
(
|
||||
n = n as string
|
||||
local newNumber = 0
|
||||
for i = 1 to n.count do
|
||||
(
|
||||
local digit = n[i] as integer
|
||||
newNumber += pow digit 2
|
||||
)
|
||||
n = newNumber
|
||||
if (finditem pastNumbers n) != 0 do return false
|
||||
append pastNumbers newNumber
|
||||
)
|
||||
n == 1
|
||||
)
|
||||
printed = 0
|
||||
for i in (for h in 1 to 500 where isHappyNumber h collect h) do
|
||||
(
|
||||
if printed == 8 do exit
|
||||
print i as string
|
||||
printed += 1
|
||||
|
||||
)
|
||||
8
Task/Happy-numbers/MAXScript/happy-numbers-2.max
Normal file
8
Task/Happy-numbers/MAXScript/happy-numbers-2.max
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
1
|
||||
7
|
||||
10
|
||||
13
|
||||
19
|
||||
23
|
||||
28
|
||||
31
|
||||
40
Task/Happy-numbers/Mercury/happy-numbers.mercury
Normal file
40
Task/Happy-numbers/Mercury/happy-numbers.mercury
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
:- module happy.
|
||||
:- interface.
|
||||
:- import_module io.
|
||||
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
:- import_module int, list, set_tree234.
|
||||
|
||||
main(!IO) :-
|
||||
print_line(get_n_happy_numbers(8, 1), !IO).
|
||||
|
||||
:- func get_n_happy_numbers(int, int) = list(int).
|
||||
|
||||
get_n_happy_numbers(NumToFind, N) =
|
||||
( if NumToFind > 0 then
|
||||
( if is_happy(N, init)
|
||||
then [N | get_n_happy_numbers(NumToFind - 1, N + 1)]
|
||||
else get_n_happy_numbers(NumToFind, N + 1)
|
||||
)
|
||||
else
|
||||
[]
|
||||
).
|
||||
|
||||
:- pred is_happy(int::in, set_tree234(int)::in) is semidet.
|
||||
|
||||
is_happy(1, _).
|
||||
is_happy(N, !.Seen) :-
|
||||
not member(N, !.Seen),
|
||||
insert(N, !Seen),
|
||||
is_happy(sum_sqr_digits(N), !.Seen).
|
||||
|
||||
:- func sum_sqr_digits(int) = int.
|
||||
|
||||
sum_sqr_digits(N) =
|
||||
( if N < 10 then sqr(N) else sqr(N mod 10) + sum_sqr_digits(N div 10) ).
|
||||
|
||||
:- func sqr(int) = int.
|
||||
|
||||
sqr(X) = X * X.
|
||||
171
Task/Happy-numbers/Pascal/happy-numbers-2.pascal
Normal file
171
Task/Happy-numbers/Pascal/happy-numbers-2.pascal
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
Program HappyNumbers (output);
|
||||
// NativeUInt: LongWord 32-Bit-OS/ Uint64 64-Bit-OS
|
||||
{$IFDEF FPC}
|
||||
{$MODE DELPHI}
|
||||
{$OPTIMIZATION ON,Regvar,PEEPHOLE,CSE,ASMCSE}
|
||||
{$CODEALIGN proc=32}
|
||||
{$ELSE}
|
||||
//for Delphi
|
||||
{$APPLICATION CONSOLE}
|
||||
{$ENDIF}
|
||||
const
|
||||
HighCache = 19*(9*9);//sum sqrdigt of Uint64
|
||||
cDigit = 1000;
|
||||
type
|
||||
tCache = array[0..HighCache] of Word;
|
||||
tSqrdCache = array[0..cDigit] of Word;
|
||||
tSqrdSumCache = array[0..HighCache] of Word;
|
||||
var
|
||||
Cache : tCache;
|
||||
SqrdCache :tSqrdCache;
|
||||
SqrdSumCache :tSqrdSumCache;
|
||||
|
||||
function find(n: NativeUint;const cache: tCache): boolean;
|
||||
var
|
||||
i: NativeUint;
|
||||
begin
|
||||
find := false;
|
||||
for i := low(cache) to high(cache) do
|
||||
if cache[i] = n then
|
||||
find := true;
|
||||
writeln(i:10,n:10);
|
||||
end;
|
||||
|
||||
procedure InitSqrdCache;
|
||||
var
|
||||
i,n,sum,r: NativeUint;
|
||||
begin
|
||||
For i := 0 to cDigit do
|
||||
Begin
|
||||
sum := 0;
|
||||
n := i;
|
||||
while n > 0 do
|
||||
begin
|
||||
r := n;
|
||||
n := n div 10;
|
||||
r := r-10*n;
|
||||
sum := sum + r*r;
|
||||
end;
|
||||
SqrdCache[i] := sum;
|
||||
end;
|
||||
end;
|
||||
|
||||
function SumSqrdDgt(n: NativeUint): NativeUint;
|
||||
var
|
||||
sum,r: NativeUint;
|
||||
begin
|
||||
sum := 0;
|
||||
while n > cDigit do
|
||||
begin
|
||||
r := n;
|
||||
n := n div cDigit;
|
||||
r := r-cDigit*n;
|
||||
sum := sum + SqrdCache[r];
|
||||
end;
|
||||
SumSqrdDgt := sum + SqrdCache[n];
|
||||
end;
|
||||
|
||||
|
||||
procedure Inithappy;
|
||||
var
|
||||
n,s,p : NativeUint;
|
||||
Begin
|
||||
fillchar(SqrdSumCache,SizeOf(SqrdSumCache),#0);
|
||||
InitSqrdCache;
|
||||
fillChar(Cache,SizeOf(Cache),#0);
|
||||
Cache[1] := 1;
|
||||
For n := 1 to High(Cache) do
|
||||
Begin
|
||||
If Cache[n] = 0 then
|
||||
Begin
|
||||
//start a linked list
|
||||
Cache[n] := n;
|
||||
p := n;
|
||||
s := SumSqrdDgt(p);
|
||||
while Cache[s] = 0 do
|
||||
Begin
|
||||
Cache[s] := p;
|
||||
p := s;
|
||||
s := SumSqrdDgt(p);
|
||||
end;
|
||||
//mark linked list backwards as happy number
|
||||
IF Cache[s] = 1 then
|
||||
Begin
|
||||
repeat
|
||||
s := Cache[p];
|
||||
Cache[p] := 1;
|
||||
p := s;
|
||||
until s = n;
|
||||
Cache[n] := 1;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
function nextCdigits(sqSum: NativeUint):NativeUint;
|
||||
var
|
||||
i,cnt : LongInt;
|
||||
Begin
|
||||
cnt:= SqrdSumCache[sqSum];
|
||||
If cnt = 0 then
|
||||
Begin
|
||||
For i := 0 to Cdigit-1 do
|
||||
cnt := cnt + Ord(Cache[sqSum+SqrdCache[i]]=1);
|
||||
//saving calculation->speed up x100
|
||||
SqrdSumCache[sqSum] := cnt;
|
||||
end;
|
||||
nextCdigits := cnt;
|
||||
end;
|
||||
|
||||
function is_happy(n: NativeUint): boolean;inline;
|
||||
begin
|
||||
is_happy := Cache[SumSqrdDgt(n)]=1
|
||||
end;
|
||||
|
||||
function nthHappy(Limit: NativeUint):NativeUint;
|
||||
var
|
||||
n,
|
||||
count : NativeUint;
|
||||
begin
|
||||
n:= 0;
|
||||
count := 0;
|
||||
// big steps
|
||||
IF limit>cDigit then
|
||||
repeat
|
||||
inc(count,nextCdigits(SumSqrdDgt(n)));
|
||||
inc(n,cDigit);
|
||||
until count >= Limit-cDigit;
|
||||
// small steps
|
||||
repeat
|
||||
if is_happy(n) then
|
||||
inc(count);
|
||||
inc(n);
|
||||
until count >= Limit;
|
||||
nthHappy:= n-1;
|
||||
end;
|
||||
|
||||
var
|
||||
n, count,Limit: NativeUint;
|
||||
begin
|
||||
Inithappy;
|
||||
n := 1;
|
||||
count := 0;
|
||||
while count < 8 do
|
||||
begin
|
||||
if is_happy(n) then
|
||||
begin
|
||||
inc(count);
|
||||
write(n, ' ');
|
||||
end;
|
||||
inc(n);
|
||||
end;
|
||||
writeln;
|
||||
|
||||
n := 1;
|
||||
Limit := 10;// 1En
|
||||
repeat
|
||||
writeln('10e',n,' nth happy number ',nthHappy(limit):13);
|
||||
inc(n);
|
||||
Limit := limit*10;
|
||||
until n> 8;
|
||||
end.
|
||||
|
|
@ -1,11 +1,12 @@
|
|||
use List::Util qw(sum);
|
||||
|
||||
sub is_happy ($)
|
||||
{for (my ($n, %seen) = shift ;; $n = sum map {$_**2} split //, $n)
|
||||
{$n == 1 and return 1;
|
||||
$seen{$n}++ and return 0;}}
|
||||
sub ishappy {
|
||||
my $s = shift;
|
||||
while ($s > 6 && $s != 89) {
|
||||
$s = sum(map { $_*$_ } split(//,$s));
|
||||
}
|
||||
$s == 1;
|
||||
}
|
||||
|
||||
for (my ($n, $happy) = (1, 0) ; $happy < 8 ; ++$n)
|
||||
{is_happy $n or next;
|
||||
print "$n\n";
|
||||
++$happy;}
|
||||
my $n = 0;
|
||||
print join(" ", map { 1 until ishappy(++$n); $n; } 1..8), "\n";
|
||||
|
|
|
|||
|
|
@ -3,31 +3,26 @@ parse arg L H . /*get optional args: low & high */
|
|||
if L=='' | L==',' then L=8 /*Not specified? Set L to default*/
|
||||
if H=='' | H==',' then do; H=L; L=1; end /*use a range for the showing.*/
|
||||
#.0=0; #.1=1; #.2=4; #.3=9; #.4=16; #.5=25; #.6=36; #.7=49; #.8=64; #.9=81
|
||||
@.=0; @.1=1; !.=@.; !.2=1; !.3=1; !.4=1 /*sparse array: @≡hap, !≡unhap*/
|
||||
haps=0 /*count of happy numbers so far. */
|
||||
@.=0; !.=0 /*sparse array: happy&unhappy #s.*/
|
||||
|
||||
do n=1 while haps<H; q=n; a.=0 /*search integers starting at 1.*/
|
||||
if !.n then iterate /*if N is unhappy, try another.*/
|
||||
do n=1 while haps<H /*search integers starting at 1.*/
|
||||
if !.n then iterate /*if N is unhappy, try another.*/
|
||||
q=n /*(below) Q is the number tested.*/
|
||||
do until q==1; s=0 /*see if Q is a happy number. */
|
||||
?=q /*note: ? is destructively PARSEd*/
|
||||
do length(q) /*parse all digs of ? (base 10).*/
|
||||
parse var ? _ +1 ? /*obtain a single digit of ? */
|
||||
s=s + #._ /*add the square of that digit.*/
|
||||
end /*length(q)*/ /* [↑] perform the DO W times.*/
|
||||
|
||||
do until q==1 /*see if Q is a happy number. */
|
||||
s=0 /*prepare to add squares of digs.*/
|
||||
do j=1 for length(q) /*sum the squares of the digits. */
|
||||
_=substr(q,j,1) /*get a single digit (in base 10)*/
|
||||
s=s+#._ /*add the square of a digit. */
|
||||
end /*j*/
|
||||
|
||||
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*/
|
||||
iterate n /*previously summed, so Q unhappy*/
|
||||
end
|
||||
a.s=1; q=s /*mark sum as found, try Q sum.*/
|
||||
end /*until*/
|
||||
|
||||
@.n=1 /*mark N as a happy number. */
|
||||
haps=haps+1 /*bump the count of happy numbers*/
|
||||
if haps<L then iterate /*don't display, N is too low. */
|
||||
say n /*display the happy N number.*/
|
||||
end /*n*/
|
||||
if !.s then iterate n /*Sum unhappy? Then Q is unhappy*/
|
||||
if @.s then leave /*we have found a happy number.*/
|
||||
q=s /*try the Q sum to see if happy.*/
|
||||
end /*until*/
|
||||
@.n=1 /*mark N as a happy number. */
|
||||
haps=haps+1 /*bump the count of happy numbers*/
|
||||
if haps<L then iterate /*don't display, N is too low. */
|
||||
say right(n, 30) /*display right justified happy #*/
|
||||
end /*n*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
|
|
@ -1,41 +1,35 @@
|
|||
/*REXX program displays eight (or a specified range of) happy numbers.*/
|
||||
sw=linesize() /*obtain the screen width of term*/
|
||||
parse arg L H . /*get optional args: low & high */
|
||||
if L=='' | L==',' then L=8 /*Not specified? Set L to default*/
|
||||
if H=='' | H==',' then do; H=L; L=1; end /*use a range for the showing.*/
|
||||
#.0=0; #.1=1; #.2=4; #.3=9; #.4=16; #.5=25; #.6=36; #.7=49; #.8=64; #.9=81
|
||||
@.=0; @.1=1; !.=@.; !.2=1; !.3=1; !.4=1 /*sparse array: @≡hap, !≡unhap*/
|
||||
haps=0 /*count of happy numbers so far. */
|
||||
@.=0; !.=0 /*sparse array: happy&unhappy #s.*/
|
||||
out= /*the output line (of happy nums)*/
|
||||
sw=linesize() /*obtain the linesize of term scr*/
|
||||
$=
|
||||
do n=1 while haps<H /*search integers starting at 1.*/
|
||||
if !.n then iterate /*if N is unhappy, try another.*/
|
||||
q=n /*(below) Q is the number tested.*/
|
||||
do until q==1; s=0 /*see if Q is a happy number. */
|
||||
?=q /*note: ? is destructively PARSEd*/
|
||||
do length(q) /*parse all digs of ? (base 10).*/
|
||||
parse var ? _ +1 ? /*obtain a single digit of ? */
|
||||
s=s + #._ /*add the square of that digit.*/
|
||||
end /*length(q)*/ /* [↑] perform the DO W times.*/
|
||||
|
||||
do n=1 while haps<H; q=n; a.=0 /*search integers starting at 1.*/
|
||||
if !.n then iterate /*if N is unhappy, try another.*/
|
||||
|
||||
do until q==1 /*see if Q is a happy number. */
|
||||
s=0 /*prepare to add squares of digs.*/
|
||||
do j=1 for length(q) /*sum the squares of the digits. */
|
||||
_=substr(q,j,1) /*get a single digit (in base 10)*/
|
||||
s=s+#._ /*add the square of a digit. */
|
||||
end /*j*/
|
||||
|
||||
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*/
|
||||
iterate n /*if already summed, Q is unhappy*/
|
||||
end
|
||||
a.s=1; q=s /*mark sum as found, try Q sum.*/
|
||||
end /*until*/
|
||||
|
||||
@.n=1 /*mark N as a happy number. */
|
||||
haps=haps+1 /*bump the count of happy numbers*/
|
||||
if haps<L then iterate /*don't display, N is too low. */
|
||||
if length(out n)>sw then do /*maybe display the happy number.*/
|
||||
say strip(out) /*line is too long, tell it*/
|
||||
out= /*nullify the OUT (line).*/
|
||||
end
|
||||
out=out n /*append the happy number to OUT.*/
|
||||
end /*n*/
|
||||
|
||||
if out\=='' then say strip(out) /*handle any residuals for OUT. */
|
||||
if !.s then iterate n /*Sum unhappy? Then Q is unhappy*/
|
||||
if @.s then leave /*we have found a happy number.*/
|
||||
q=s /*try the Q sum to see if happy.*/
|
||||
end /*until*/
|
||||
@.n=1 /*mark N as a happy number. */
|
||||
haps=haps+1 /*bump the count of happy numbers*/
|
||||
if haps<L then iterate /*don't display, N is too low. */
|
||||
$=$ n /*add N to the horizontal list.*/
|
||||
if length($ n)>sw then do /*if the list is too long, split */
|
||||
say strip($) /*and display what we've got.*/
|
||||
$=n /*set next line to overflow. */
|
||||
end /* [↑] now contains overlow.*/
|
||||
end /*n*/
|
||||
if $\='' then say strip($) /*display any residual happy nums*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
def print_happy
|
||||
happy_numbers = []
|
||||
|
||||
(1..Float::INFINITY).each do |i|
|
||||
1.step do |i|
|
||||
break if happy_numbers.length >= 8
|
||||
happy_numbers << i if happy?(i)
|
||||
end
|
||||
|
||||
p happy_numbers
|
||||
end
|
||||
|
||||
print_happy
|
||||
|
|
|
|||
18
Task/Happy-numbers/Ruby/happy-numbers-4.rb
Normal file
18
Task/Happy-numbers/Ruby/happy-numbers-4.rb
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
@memo = [0,1]
|
||||
def happy(n)
|
||||
sum = n.to_s.chars.map{|c| c.to_i**2}.inject(:+)
|
||||
return @memo[sum] if @memo[sum]==0 or @memo[sum]==1
|
||||
@memo[sum] = 0 # for the cycle check
|
||||
@memo[sum] = happy(sum) # return 1:Happy number, 0:other
|
||||
end
|
||||
|
||||
i = count = 0
|
||||
while count < 8
|
||||
i += 1
|
||||
puts i or count+=1 if happy(i)==1
|
||||
end
|
||||
|
||||
puts
|
||||
for i in 99999999999900..99999999999999
|
||||
puts i if happy(i)==1
|
||||
end
|
||||
36
Task/Happy-numbers/Rust/happy-numbers.rust
Normal file
36
Task/Happy-numbers/Rust/happy-numbers.rust
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#![feature(core)]
|
||||
|
||||
fn sumsqd(mut n: i32) -> i32 {
|
||||
let mut sq = 0;
|
||||
while n > 0 {
|
||||
let d = n % 10;
|
||||
sq += d*d;
|
||||
n /= 10
|
||||
}
|
||||
sq
|
||||
}
|
||||
|
||||
use std::num::Int;
|
||||
fn cycle<T: Int>(a: T, f: fn(T) -> T) -> T {
|
||||
let mut t = a;
|
||||
let mut h = f(a);
|
||||
|
||||
while t != h {
|
||||
t = f(t);
|
||||
h = f(f(h))
|
||||
}
|
||||
t
|
||||
}
|
||||
|
||||
fn ishappy(n: i32) -> bool {
|
||||
cycle(n, sumsqd) == 1
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let happy = std::iter::count(1, 1)
|
||||
.filter(|&n| ishappy(n))
|
||||
.take(8)
|
||||
.collect::<Vec<i32>>();
|
||||
|
||||
println!("{:?}", happy)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue