Data update
This commit is contained in:
parent
29a5eea0d4
commit
5c1bb7bfa9
2011 changed files with 35081 additions and 3229 deletions
109
Task/Weird-numbers/Ada/weird-numbers-1.ada
Normal file
109
Task/Weird-numbers/Ada/weird-numbers-1.ada
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
pragma Ada_2022;
|
||||
|
||||
with Ada.Text_IO;
|
||||
with Ada.Containers.Vectors;
|
||||
with Ada.Containers.Ordered_Sets;
|
||||
|
||||
procedure Weird_Numbers is
|
||||
package IO renames Ada.Text_IO;
|
||||
-- allows us to type "IO.Put_Line" instead of "Ada.Text_IO.Put_Line"
|
||||
|
||||
package IntVecs is new Ada.Containers.Vectors
|
||||
(Index_Type => Positive, Element_Type => Natural);
|
||||
subtype IntVec is IntVecs.Vector;
|
||||
-- use all type IntVec;
|
||||
|
||||
Factor_Cache : IntVec;
|
||||
-- used to keep track of the factors on each iteration
|
||||
|
||||
package Intsets is new Ada.Containers.Ordered_Sets
|
||||
(Element_Type => Positive);
|
||||
subtype Intset is Intsets.Set;
|
||||
Semiperfect_Cache : Intset;
|
||||
Not_Abundant_Cache : Intset;
|
||||
|
||||
function Proper_Divisors (Value : Positive) return IntVec is
|
||||
Cofactors : IntVec;
|
||||
begin
|
||||
Factor_Cache.Clear;
|
||||
begin
|
||||
Factor_Cache.Append (1);
|
||||
for Factor in 2 .. Value / 2 loop
|
||||
if Value rem Factor = 0 then
|
||||
Factor_Cache.Append (Factor);
|
||||
end if;
|
||||
end loop;
|
||||
return Factor_Cache;
|
||||
end;
|
||||
end Proper_Divisors;
|
||||
|
||||
function Is_Abundant (Value : Positive) return Boolean is
|
||||
begin
|
||||
if Not_Abundant_Cache.Contains (Value)
|
||||
or else Semiperfect_Cache.Contains (Value)
|
||||
then
|
||||
return False;
|
||||
end if;
|
||||
if Proper_Divisors (Value)'Reduce ("+", 0) > Value then
|
||||
return True;
|
||||
end if;
|
||||
Not_Abundant_Cache.Insert (Value);
|
||||
return False;
|
||||
end Is_Abundant;
|
||||
|
||||
function Subset_Of (Factors : IntVec; Ith : Positive) return IntVec is
|
||||
-- returns the Ith subset of Factors
|
||||
-- if Factors has N elements, then there are 2**N possibilities
|
||||
-- this maps Ith to one of these possibilities using its binary representation
|
||||
Result : IntVec;
|
||||
Index : Natural := 1;
|
||||
Ith_Remainder : Natural := Ith;
|
||||
begin
|
||||
while Ith_Remainder /= 0 loop
|
||||
if Ith_Remainder rem 2 = 1 then
|
||||
Result.Append (Factors (Index));
|
||||
Ith_Remainder := @ - 1;
|
||||
end if;
|
||||
Ith_Remainder := @ / 2;
|
||||
Index := @ + 1;
|
||||
end loop;
|
||||
return Result;
|
||||
end Subset_Of;
|
||||
|
||||
function Is_Semiperfect (Value : Positive) return Boolean is
|
||||
Factors : constant IntVec := Factor_Cache;
|
||||
Subset : IntVec;
|
||||
Sum : Natural;
|
||||
begin
|
||||
if (for some Previous of Semiperfect_Cache => Value rem Previous = 0)
|
||||
then
|
||||
return True;
|
||||
end if;
|
||||
for Ith in reverse 1 .. (2**Positive (Factors.Length) - 1) loop
|
||||
Subset := Subset_Of (Factors, Positive (Ith));
|
||||
Sum := Subset'Reduce ("+", 0);
|
||||
if Sum = Value then
|
||||
Semiperfect_Cache.Insert (Value);
|
||||
return True;
|
||||
end if;
|
||||
end loop;
|
||||
return False;
|
||||
end Is_Semiperfect;
|
||||
|
||||
function Is_Weird (Value : Positive) return Boolean is
|
||||
(Is_Abundant (Value) and then not Is_Semiperfect (Value));
|
||||
|
||||
Current : Positive := 2;
|
||||
Number_Found : Natural := 0;
|
||||
|
||||
begin
|
||||
IO.Put ("The first 25 weird numbers are");
|
||||
while Number_Found < 25 loop
|
||||
if Is_Weird (Current * 2) then
|
||||
IO.Put (Integer'Image (Current * 2));
|
||||
Number_Found := @ + 1;
|
||||
end if;
|
||||
Current := @ + 1;
|
||||
end loop;
|
||||
IO.New_Line;
|
||||
end Weird_Numbers;
|
||||
105
Task/Weird-numbers/Ada/weird-numbers-2.ada
Normal file
105
Task/Weird-numbers/Ada/weird-numbers-2.ada
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
pragma Ada_2022;
|
||||
|
||||
with Ada.Text_IO;
|
||||
with Ada.Containers.Vectors;
|
||||
with Ada.Containers.Ordered_Sets;
|
||||
|
||||
procedure Weird_Numbers is
|
||||
package IO renames Ada.Text_IO;
|
||||
|
||||
package IntVecs is new Ada.Containers.Vectors
|
||||
(Index_Type => Positive, Element_Type => Positive);
|
||||
subtype IntVec is IntVecs.Vector;
|
||||
use all type IntVec;
|
||||
|
||||
Factor_Cache : IntVec;
|
||||
|
||||
package Intsets is new Ada.Containers.Ordered_Sets
|
||||
(Element_Type => Positive);
|
||||
subtype Intset is Intsets.Set;
|
||||
Semiperfect_Cache : Intset;
|
||||
Not_Abundant_Cache : Intset;
|
||||
|
||||
function Proper_Divisors (Value : Positive) return IntVec is
|
||||
Cofactors : IntVec;
|
||||
begin
|
||||
Factor_Cache.Clear;
|
||||
begin
|
||||
Factor_Cache.Append (1);
|
||||
for Factor in 2 .. Value / 2 loop
|
||||
if Factor * Factor > Value then
|
||||
exit;
|
||||
end if;
|
||||
if Value rem Factor = 0 then
|
||||
Factor_Cache.Append (Factor);
|
||||
if Factor * Factor /= Value then
|
||||
Cofactors.Append (Value / Factor);
|
||||
end if;
|
||||
end if;
|
||||
end loop;
|
||||
Factor_Cache.Prepend_Vector (Cofactors);
|
||||
-- this ordering is REALLY curious
|
||||
-- for instance, the factors of 12 are ordered as 6, 4, 1, 2, 3
|
||||
-- AND THIS MATTERS
|
||||
-- both strictly ascending and strictly descending orders are much slower
|
||||
return Factor_Cache;
|
||||
end;
|
||||
end Proper_Divisors;
|
||||
|
||||
function Is_Abundant (Value : Positive) return Boolean is
|
||||
begin
|
||||
if Not_Abundant_Cache.Contains (Value)
|
||||
or else Semiperfect_Cache.Contains (Value)
|
||||
then
|
||||
return False;
|
||||
end if;
|
||||
if Proper_Divisors (Value)'Reduce ("+", 0) > Value then
|
||||
return True;
|
||||
end if;
|
||||
Not_Abundant_Cache.Insert (Value);
|
||||
return False;
|
||||
end Is_Abundant;
|
||||
|
||||
function Alternate_Semiperfect (Value : Positive) return Boolean is
|
||||
function Reroute
|
||||
(Value : Positive; First : Positive) return Boolean
|
||||
is
|
||||
Head : Positive;
|
||||
begin
|
||||
if Factor_Cache.Last_Index >= First then
|
||||
Head := Factor_Cache (First);
|
||||
if Value < Head then
|
||||
return Reroute (Value, First + 1);
|
||||
else
|
||||
return
|
||||
Value = Head
|
||||
or else Reroute (Value - Head, First + 1)
|
||||
or else Reroute (Value, First + 1);
|
||||
end if;
|
||||
else
|
||||
return False;
|
||||
end if;
|
||||
end Reroute;
|
||||
begin
|
||||
return
|
||||
Reroute
|
||||
(Value, Factor_Cache.First_Index);
|
||||
end Alternate_Semiperfect;
|
||||
|
||||
function Is_Weird (Value : Positive) return Boolean is
|
||||
(Is_Abundant (Value) and then not Alternate_Semiperfect (Value));
|
||||
|
||||
Current : Positive := 2;
|
||||
Number_Found : Natural := 0;
|
||||
|
||||
begin
|
||||
IO.Put ("The first 25 weird numbers are");
|
||||
while Number_Found < 25 loop
|
||||
if Is_Weird (Current * 2) then
|
||||
IO.Put (Integer'Image (Current * 2));
|
||||
Number_Found := @ + 1;
|
||||
end if;
|
||||
Current := @ + 1;
|
||||
end loop;
|
||||
IO.New_Line;
|
||||
end Weird_Numbers;
|
||||
51
Task/Weird-numbers/Python/weird-numbers-2.py
Normal file
51
Task/Weird-numbers/Python/weird-numbers-2.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
# anySum :: Int -> [Int] -> [Int]
|
||||
|
||||
from time import time
|
||||
start = time()
|
||||
|
||||
primitivesp_nos = set(); primes = []
|
||||
|
||||
def main():
|
||||
x = 2; weird_nos = []
|
||||
n = 50
|
||||
while n > 0:
|
||||
if isweird(x) == 1: weird_nos.append(x); n = n - 1
|
||||
x = x + 1
|
||||
print("First", len(weird_nos), "weird nos:\n", weird_nos)
|
||||
|
||||
def isweird(n):
|
||||
global primes; global primitivesp_nos; pr_fctrs = []; a = n
|
||||
for i in primes:
|
||||
while a % i == 0: pr_fctrs.append(i); a = a//i
|
||||
if i * i > a: break
|
||||
if a > 1: pr_fctrs.append(a)
|
||||
if a == n: primes.append(n); return 0
|
||||
sum_fctrs = 1; divisors = set(pr_fctrs)
|
||||
for i in divisors: sum_fctrs = sum_fctrs*(i**(pr_fctrs.count(i)+1)-1)//(i-1)
|
||||
difference = sum_fctrs - 2 * n
|
||||
if difference <= 0:
|
||||
if difference == 0: primitivesp_nos.add(n)
|
||||
return 0
|
||||
# Next 10 lines from Jerome Richard: stackoverflow.com/questions/6800193
|
||||
divisors = [1]; last_prime = 0; fctr = 0; slice_len = 0
|
||||
for prime in pr_fctrs:
|
||||
if last_prime != prime: slice_len = len(divisors); fctr = prime
|
||||
else: fctr *= prime
|
||||
for i in range(slice_len):
|
||||
d = divisors[i] * fctr
|
||||
if d not in primitivesp_nos: divisors.append(d)
|
||||
else: return 0
|
||||
last_prime = prime
|
||||
x = 1; divisors = set(i for i in divisors if i <= difference)
|
||||
ns = n - (difference + n - sum(divisors))
|
||||
if ns < 0: return 1
|
||||
for d in divisors: x |= x << d
|
||||
if x >> ns & 1: primitivesp_nos.add(n); return 0
|
||||
else: return 1
|
||||
|
||||
main()
|
||||
|
||||
end = time()
|
||||
print("Execution time: ", round(end - start, 2), "s")
|
||||
|
||||
-- -> [100,96]
|
||||
85
Task/Weird-numbers/Uxntal/weird-numbers.uxnatl
Normal file
85
Task/Weird-numbers/Uxntal/weird-numbers.uxnatl
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
( uxncli weird.rom )
|
||||
|
||||
|10 @Console &vector $2 &read $1 &pad $5 &write $1 &error $1
|
||||
%MOD2 { DIV2k MUL2 SUB2 }
|
||||
|0000
|
||||
@c $2
|
||||
@n $2
|
||||
@i $1
|
||||
|
||||
|0100
|
||||
( init )
|
||||
;str ptext #0a .Console/write DEO
|
||||
|
||||
( main )
|
||||
#000c
|
||||
&sieve
|
||||
#0000 .c STZ2
|
||||
DUP2 DUP2 .n STZ2 divisors
|
||||
abundance ?{ semiperfection }
|
||||
clear
|
||||
INC2 .i LDZ #19 LTH ?&sieve
|
||||
POP2
|
||||
BRK
|
||||
|
||||
@pdec ( short* -- )
|
||||
#2710 [ LIT2r 00fb ]
|
||||
&while ( -- )
|
||||
DIV2k #000a DIV2k MUL2 SUB2 SWPr EQUk OVR STHkr EQU AND ?{
|
||||
DUP [ LIT "0 ] ADD #19 DEO
|
||||
INCr }
|
||||
POP2 #000a DIV2 SWPr INCr STHkr ?&while
|
||||
POP2r POP2 POP2 JMP2r
|
||||
|
||||
@ptext ( str* -- )
|
||||
&while
|
||||
LDAk .Console/write DEO
|
||||
INC2 LDAk ?&while
|
||||
POP2
|
||||
JMP2r
|
||||
|
||||
@divisors ( short* -- )
|
||||
#0000
|
||||
&loop
|
||||
OVR2 OVR2 MOD2 #0000 GTH2 ?{ DUP2 ;divs .c LDZ2 STH2k ADD2 STA2 STH2r INC2 INC2 .c STZ2 }
|
||||
INC2 GTH2k ?&loop POP2 POP2
|
||||
JMP2r
|
||||
|
||||
@abundance ( -- flag )
|
||||
#0000 STH2
|
||||
;divs
|
||||
&while
|
||||
LDA2k STH2 ADD2r
|
||||
#0002 ADD2
|
||||
LDA2k NIP ?&while
|
||||
POP2
|
||||
STH2r .n LDZ2 GTH2 ?{ #01 JMP2r } #00 ( returns 1 when not abundant to skip semiperfection check in main )
|
||||
JMP2r
|
||||
|
||||
@semiperfection ( -- )
|
||||
#0001 ;divs .c LDZ2 ADD2 STA2
|
||||
#0000
|
||||
&for
|
||||
.n LDZ2
|
||||
&loop
|
||||
OVR2 OVR2 SWP2 #0002 MUL2 ;divs ADD2 LDA2 SUB2 #0002 MUL2
|
||||
;divs .c LDZ2 ADD2 ADD2 LDA2
|
||||
#0000 EQU2 ?{ #0001 OVR2 #0002 MUL2 ;divs .c LDZ2 ADD2 ADD2 STA2 }
|
||||
#0001 SUB2
|
||||
OVR2 #0002 MUL2 ;divs ADD2 LDA2 GTH2k STH EQU2k STHr ORA NIP NIP ?&loop POP2
|
||||
|
||||
INC2 DUP2 .c LDZ2 #0002 DIV2 LTH2 ?&for POP2
|
||||
;divs .c LDZ2 ADD2 .n LDZ2 #0002 MUL2 ADD2 LDA2 NIP ?{ .n LDZ2 pdec #20 #18 DEO .i LDZk INC SWP STZ JMP2r }
|
||||
JMP2r
|
||||
|
||||
@clear ( -- )
|
||||
;divs
|
||||
.n LDZ2 .c LDZ2 ADD2
|
||||
&while
|
||||
SWP2 #0000 SWP2 STA2k NIP2
|
||||
INC2 INC2
|
||||
SWP2 #0001 SUB2 #0000 GTH2k NIP NIP ?&while POP2 POP2
|
||||
JMP2r
|
||||
|
||||
@str "The 20 "first 20 "25 20 "weird 20 "numbers: 00
|
||||
@divs
|
||||
Loading…
Add table
Add a link
Reference in a new issue