Weird numbers: Difference between revisions

Content deleted Content added
Cantanima (talk | contribs)
Nosach (talk | contribs)
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 117:
 
package IntVecs is new Ada.Containers.Vectors
(Index_Type => Positive, Element_Type => PositiveNatural);
subtype IntVec is IntVecs.Vector;
-- use all type IntVec;
 
Factor_Cache : IntVec;
Line 215:
IO.New_Line;
end Weird_Numbers;
 
</syntaxhighlight>
 
Line 2,627 ⟶ 2,626:
 
Edit: I experimented a bit and created this second version. It's faster than the original, takes about 0.16 s for first 50.
Edit2: I made some small changes again to make it a little faster still.
 
::<syntaxhighlight lang="python"># anySum :: Int -> [Int] -> [Int]
Line 2,632:
from time import time
start = time()
 
from itertools import combinations
from math import prod
 
primitivesp_nos = set(); primes = []
 
def main():
x = 2; weird_nos = []
n = 176650
x = 1
while n > 0:
if isweird(x) == 1: weird_nos.append(x); n = n - 1
Line 2,648 ⟶ 2,644:
 
def isweird(n):
global primes; global primitivesp_nos; xpr_fctrs = []; a = n
for i in primes:
while a % i == 0: xpr_fctrs.append(i); a = a//i
if i * i > a: break
if a > 1: xpr_fctrs.append(a)
if xa == [n]: primes.append(n); return 0
sum_fctrs = 1; fctrsdivisors = set(xpr_fctrs)
for i in fctrsdivisors: sum_fctrs = sum_fctrs*(i**(xpr_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
for i in range(2, len(x)):
divisors = [1]; last_prime = 0; fctr = 0; slice_len = 0
for j in combinations(x, i):
for prime in pr_fctrs:
product = prod(j)
if last_prime != prime: slice_len = len(divisors); fctr = prime
if product not in primitivesp_nos: fctrs.add(product)
else: returnfctr *= 0prime
x = 1; fctrs = {1}.union(set(i for i in fctrs if i <= difference)range(slice_len):
ns = n - (difference + n - sum(fctrs))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 fctrsdivisors: x |= x << d
if not x >> ns & 1: primitivesp_nos.add(n); return 10
else: primitivesp_nos.add(n); return 01
main()
Line 2,676 ⟶ 2,677:
end = time()
print("Execution time: ", round(end - start, 2), "s")
 
-- -> [100,96]</syntaxhighlight>
 
Line 3,191 ⟶ 3,193:
var w = (1..Inf -> lazy.grep(is_weird).first(25))
say "The first 25 weird numbers:\n#{w.join(' ')}"</syntaxhighlight>
{{out}}
<pre>
The first 25 weird numbers:
70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310
</pre>
 
=={{header|Uxntal}}==
<syntaxhighlight lang="uxntal">( 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</syntaxhighlight>
{{out}}
<pre>