Data update

This commit is contained in:
Ingy döt Net 2024-03-06 22:25:12 -08:00
parent ed705008a8
commit 0df55f9f24
2196 changed files with 32999 additions and 3075 deletions

View file

@ -0,0 +1,27 @@
func dsum n .
while n > 0
d = n mod 10
s += d * d
n = n div 10
.
return s
.
func happy n .
while n > 999
n = dsum n
.
len seen[] 999
repeat
n = dsum n
until seen[n] = 1
seen[n] = 1
.
return if n = 1
.
while cnt < 8
n += 1
if happy n = 1
cnt += 1
write n & " "
.
.

View file

@ -9,14 +9,14 @@ isHappy(int n)
int num := n;
while (num != 1)
{
if (cache.indexOfElement:num != -1)
if (cache.indexOfElement(num) != -1)
{
^ false
};
cache.append(num);
while (num != 0)
{
int digit := num.mod:10;
int digit := num.mod(10);
sum += (digit*digit);
num /= 10
};

View file

@ -1,13 +1,14 @@
function happy(x)
happy_ints = ref(Int)
happy_ints = Int[]
int_try = 1
while length(happy_ints) < x
n = int_try
past = ref(Int)
past = Int[]
while n != 1
n = sum([y^2 for y in digits(n)])
contains(past,n) ? break : push!(past,n)
end
n = sum(y^2 for y in digits(n))
n in past && break
push!(past, n)
end
n == 1 && push!(happy_ints,int_try)
int_try += 1
end

View file

@ -1,11 +1,10 @@
sumhappy(n) = sum(x->x^2, digits(n))
function ishappy(x, mem = [])
x == 1? true :
x in mem? false :
ishappy(sumhappy(x),[mem ; x])
function ishappy(x, mem = Int[])
x == 1 ? true :
x in mem ? false :
ishappy(sumhappy(x), [mem ; x])
end
nexthappy (x) = ishappy(x+1) ? x+1 : nexthappy(x+1)
happy(n) = [z = 1 ; [z = nexthappy(z) for i = 1:n-1]]
nexthappy(x) = ishappy(x+1) ? x+1 : nexthappy(x+1)
happy(n) = accumulate((a, b) -> nexthappy(a), 1:n)

View file

@ -1,30 +1,30 @@
const CACHE = 256
buf = zeros(Int,CACHE)
buf[1] = 1
#happy(n) returns 1 if happy, 0 if not
buf = zeros(Int, CACHE)
buf[begin] = 1
function happy(n)
if n < CACHE
buf[n] > 0 && return 2-buf[n]
buf[n] = 2
end
sum = 0
sqsum = 0
nn = n
while nn != 0
x = nn%10
sum += x*x
nn = int8(nn/10)
nn, x = divrem(nn, 10)
sqsum += x * x
end
x = happy(sum)
n < CACHE && (buf[n] = 2-x)
x = happy(sqsum)
n < CACHE && (buf[n] = 2 - x)
return x
end
function main()
i = 1; counter = 1000000
i, counter = 1, 1000000
while counter > 0
if happy(i) == 1
if happy(i) != 0
counter -= 1
end
i += 1
end
return i-1
return i - 1
end

View file

@ -1,19 +0,0 @@
/*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; 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.*/
end /*until*/
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. */

View file

@ -1,27 +0,0 @@
/*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 /*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. */

View file

@ -1,34 +0,0 @@
/*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. */
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. */

View file

@ -0,0 +1,78 @@
/*REXX program computes and displays a specified range of happy numbers. */
Call time 'R'
linesize=80
Parse Arg low high /* obtain range of happy numbers */
If low='?' Then Call help
If low='' Then low=10
If high='' Then
Parse Value 1 low With low high
Do i=0 To 9 /*build a squared decimal digit table. */
square.i=i*i
End
happy.=0 /* happy.m=1 - m is a happy number */
unhappy.=0 /* unhappy.n=1 - n is an unhappy number*/
hapn=0 /* count of the happy numbers */
ol=''
Do n=1 While hapn<high /* test integers starting with 1 */
If unhappy.n Then /* if n is unhappy, */
Iterate /* then try next number */
work=n
suml='' /* list of computed sums */
Do Forever
sum=0
Do length(work) /* compute sum of squared digits */
Parse Var work digit +1 work
sum=sum+square.digit
End
Select
When unhappy.sum |, /* sum is known to be unhappy */
wordpos(sum,suml)>0 Then Do /* or was already encountered */
-- If wordpos(sum,suml)>0 Then say 'Loop' n':' suml sum
-- If n<7 Then say n':' suml sum
unhappy.n=1 /* n is unhappy */
Call set suml /* amd so are all sums so far */
Iterate n
End
When sum=1 Then Do /* we reached sum=1 */
hapn+=1 /* increment number of happy numbers */
happy.n=1 /* n is happy */
If hapn>=low Then /* if it is in specified range */
Call out n /* output it */
If hapn=high Then /* end of range reached */
Leave n /* we are done */
Iterate n /* otherwise proceed */
End
Otherwise Do /* otherwise */
suml=suml sum /* add sum to list of sums */
work=sum /* proceed with the new sum */
End
End
End
End
If ol>'' Then /* more output data */
Say strip(ol) /* write to console */
-- Say time('E')
Exit
set: /* all intermediate sums are unhappy */
Parse Arg list
Do While list<>''
Parse Var list s list
unhappy.s=1
End
Return
out: /* output management */
Parse Arg hn /* the happy number */
If length(ol hn)>linesize Then Do /* if it does not fit */
Say strip(ol) /* output the line */
ol=hn /* and start a new line */
End
Else /* otherwise */
ol=ol hn /* append is to the output line */
Return
help:
Say 'rexx hno n compute and show the first n happy numbers'
Say 'rexx hno low high show happy numbers from index low to high'
Exit

View file

@ -0,0 +1,26 @@
$ENTRY Go {
= <ShowFirst 8 Happy 1>;
};
ShowFirst {
0 s.F s.I = ;
s.N s.F s.I, <Mu s.F s.I>: T =
<Prout s.I>
<ShowFirst <- s.N 1> s.F <+ s.I 1>>;
s.N s.F s.I =
<ShowFirst s.N s.F <+ s.I 1>>;
};
Happy {
1 e.X = T;
s.N e.X s.N e.Y = F;
s.N e.X = <Happy <SqDigSum s.N> s.N e.X>;
};
SqDigSum {
0 = 0;
s.N, <Symb s.N>: s.Ds e.Rs,
<Numb s.Ds>: s.D,
<Numb e.Rs>: s.R =
<+ <* s.D s.D> <SqDigSum s.R>>;
};