Being a newbe to Python I am trying to convert the
basic program below to Python with no success.
I have converted others but this one is giving
me trouble with a capital (T).
What it does is, it create a sequence of composites
starting with 9.
9
+3+2+1+3+2
-2
+4+3+2+
-6
+5+4+3+2+
-11
+6+5+4+3+2+
-17
+7+6+5+4+3+2+
-24
+8+7+6+5+4+3+2+
-32
+9..etc.
As you can observe after -2 an algorithm can be easy
to impliment which I do in the basic code below.
The trouble with basic the array has a limit.
So looking at lists,ranges and tuples I am confused
because this sequence created its own index in the
array.
The printout of the sequence --
The array starts in index (1) wirh a (0).
0,0,0,0,0,0,0,0,9,0,0,12,0,14,15,0,0,18,0,20,21,22,0,24,
25,26,27,0,0,30,0,0,33,34,35,36,0,38,39,40,0,42,0,44,45,
46,0,48,49,50,51,52,0,54,55,56,57,58,0,60,0,62,63,0,65,66,
0,68,69,70,0,72,0,74,75,76,77,78,0,80,81,82,0,84,85,86,87,
88,0,90,91,92,93,94,95,96,0,98,99,100,0,102,0,104,105,106,0,...
Note that all the odd primes and only odd primes in index
prime, no exceptions except (1), along with (2) are all zeros.
Probably because (1) is neither a prime or a composite
Change line# 200 and 210 in basic below to get the above sequence.
200 FOR I=1 TO 7500
210 Print A(I);:J=J+1
Right now line# 200 and 210 is just setup to print odd primes
where the odd index is =(0).
No matter how large the array it will print in
sequence all odd primes up to the limit index of the array.
As below the last prime it prints is (7499)
where J = only 949 odd primes.
What I am experimenting with is starting at a much
higher summation where the veribals will be set
for that first large summation.
Basic can not do this because of the limit of
the index of the array.
So I have unlimted lists and/or ranges and/or tuples
with Python and the power of very large numbers with GMPY.
So how easy is this to convert to Python?
Program in gwbasic below.
10 CLS
15 N=-2:N1=-4:N2=-1:N4=4:N5=1
20 DIM A(7500)
30 FOR I = 1 TO 6
40 READ A1
45 ON I GOSUB 50,60,70,80,90,100
47 goto 110
50 A(A1)=A1:B1=A1:return
60 A1=A1+B1:A(A1)=A1:B1=A1:return
70 A1=A1+B1:A(A1)=A1:B1=A1:return
80 A1=A1+B1:A(A1)=A1:B1=A1:return
90 A1=A1+B1:A(A1)=A1:B1=A1:return
100 A1=A1+B1:A(A1)=A1:B1=A1:return
110 NEXT I
120 A1=B1+N:A(A1)=A1:B1=A1
130 N=N+N1
140 FOR I = N4 TO 2 STEP -1
150 A1=A1+I:IF A1>7500 THEN 160 ELSE A(A1)=A1
160 NEXT I
170 A1=A1+N:IF A1>7500 THEN 180 ELSE A(A1)=A1
180 N4=N4+N5:N=N+(-N4):X=X+1
190 IF X=7500 THEN 200 ELSE 140
200 FOR I=1 TO 7500 STEP 2
210 IF A(I) = 0 AND I>1 THEN PRINT I;:J=J+1
220 NEXT I
230 PRINT"Total odd primes =";J: END
2000 DATA 9,3,2,1,3,2
Yes it is very slow but because it is an algorithm
after -2 in the summations and negations this can
be started at a much higher index in an array (list).
So in effect find a very large prime and all there after
up too your stopping point without any lookup table
of smaller primes.
It is just fasinating it produces primes or should say
the lack therof through a simple algorithm that needs
no lookup table.
I'm stummped on the conversion to Python though.
Thanks, any help will be welcome!
Nothing to fancy please, I am learning.
Dan


|