import q6_eratos as q6e
import q6_func as q6f
Nmax = 21000000
prime_table = [ Truefor i inrange(Nmax)]
prime_table[0:2] = [False, False, True]
prime_table = q6e.eratos(prime_table,Nmax)
# Negative-integer check
IMAX=101print("\n f max",q6f.f(-IMAX))
for i inrange(IMAX):
im = -i
fm = q6f.f(im)
if fm > 0:
print(im,fm,prime_table[fm])
# Positive-integer check
IMAX=271print("\n f max",q6f.f(IMAX))
for i inrange(IMAX):
fi = q6f.f(i)
if prime_table[fi] :
print(i,fi,prime_table[fi])
% python3 q6_main.py
Number of primes found: 1329943
Largest prime found: 20999999
f max 1134331
-3 3 True
-4 16 False
-5 25 False
-6 24 False
-7 7 True
f max 20642341
1 31 True
ip=0for nj inrange(Ny):
for ni inrange(Nx):
np = int(prime_data[nj][ni])
for i inrange(ip,np+1):
if i != np and prime_table[i] == True:
print(" Error A: ", nj, ni, i, ip, np)
sys.exit()
if i == np and prime_table[i] == False:
print(" Error B: ", nj, ni, i, ip, np)
sys.exit()
print(" {0:7d} {1:7d}".format(np,i), end='')
ip = np + 1print()
print()
print('Success: There is no inconsistency\n')
print("Prime-number search completed.")
Summary of the previous article: numerical investigations were made on the largest integer that a given computer language can handle, such as the C language, FORTRAN and Python. The largest integer in FORTRAN (based on the 128-bit expression) was already impressive, but the one handled by Python was even more impressive: it has NO limitation in principle. The largest number that Python can handle can go as far as the hardware of a PC/mac configuration can allow it. Amazing.
Using pointers, one can investigate what type of representation is employed for the integer expression in computers. However, it is widely believed that the concept of the pointer is intrinsic to the C language, so that it seems at first glance that the other languages are not suitable for this approach.
In the last article, I nevertheless managed to find ways to enable this investigation with the pointers even in Fortan95 and Python. A special note is that Python employs representations of integers with 256-bit (32byte) format by default, which is already gigantic. Therefore, storing even a single integer requires 32 bytes, four times greater than an ordinary integer number in C. In this sense, Python is a modern "kid" living in the world where plenty of resources are available for users with reasonable prices.
My original plan was to write a code in Python, which is based on the algorythm called the Sieve of Eratosthnes, in order to solve the Maths problem in the entrance examination 2024 of University of Tokyo. After those playful technical attempts and detours, I finally come to a point that I can show you the code.
A code for the Sieve of Eratosthenes
I try to be as pedantic as possible to the original procedure of the Sieve of Eratosthenes.
Namely, a table containing integer numbers, say, up to 10000. Then, 0 and 1 are slashed in the table because they are not prime number by definition.
Next, the first prime number "2" is chosen and its multipliers (including 2 itself) are all marked in the table with a slash. In this process, all even integers except 2 are excluded from the list of prime numbers.
The smallest integer surviving the previous processes is the second prime number, which turns out to be "3". The multipliers of 3 are again slashed in the table.
The then surviving smallest integer (that is, 5) is the third prime number and its multipliers are .... these processes are repeated until the smallest integer surviving the previous processes exceeds the largest integer originally set in the table.
In the end, we have a list of prime numbers included in the table.
Our attempt is to write a code in our own way based on the original algorithm, but this means that there can be errors in the code. To verify what we obtained with the code are correct, we crosscheck the database provided by the PrimePages, the online database created mainly by mathematicians in the University of Tennessee. The PrimePages provides a table containing the first 1000 small primes, so that we use the data for the verification.
I ran the code several times and learned that it is necessary to set the maximum integer in the initial table needs to exceed Nmax=110000 if we demand the first 10000 smallest prime numbers. So, we begin with writing the code as,
Nmax = 110000
Npmax = 10000
We define a python list named prime_table as the table of integers up to Nmax.
prime_table = [Truefor i inrange(Nmax)]
prime_table[0:2] = [0,0,1]
The list is for a logic variable and the index of prime_table corresponds to the integers in the table. That is, if prime_table[n]=True, then the integer n is one of the prime numbers. If the list returns a false value, then the number does not belong to the prime. The first two elements of the list are set "False" (0) in the initial setting, because n=0,1 are not prime numbers. On the other hand, prime_table[2]=1 (True) is set because n=2 is a prime number.
In the begining, MaxPime=2 is set and its multipliers MaxPrime * n are to be "slashed", that is, set be "False" in the list prime_table. Due to a commutability of a product for two integers, the multipliers can be larger than and equal to MaxPrime. Pmax records the total number of the prime numbers found so far.
Pmax = 1
MaxPrime = 2while MaxPrime < Nmax:
n = MaxPrime
while n <= Nmax / MaxPrime:
prime_table[MaxPrime * n] = 0
n += 1whileTrue:
MaxPrime += 1if MaxPrime > Nmax-1:
print("End of Search at tmp MaxPrime =", MaxPrime)
breakif prime_table[MaxPrime] == True:
Pmax += 1break
In the end of the programme, a search for the next prime number, that is, MaxPrime, is attempted by referring to the data_table having the True value.
This is the contents of the Sieve of Eratosthenes.
To show the prime numbers we found with this algorithm, the additional section is placed right after the above code.
for i inrange(Nmax):
if data_table[i] == True:
print(i)
Test run
As a test, we set a pair of small numbers for Nmax and Npmax and run the code.
The first 30 prime numbers are detected for Nmax less than and equal to 120. These prime numbers are verified with the database downloaded from the PrimePages! So, it seems the code works properly. But we need to repeat this check for larger numbers, too. That would be the topic of the next article.
#include <stdio.h>#include <limits.h>intmain(){
printf("Min for signed int: %d\n", INT_MIN);
printf("Max for signed int: %d\n", INT_MAX);
printf("Max for unsigned int: %u\n", UINT_MAX);
}
実行結果は次のようになった。
Min for signed int: -2147483648
Max for signed int: 2147483647
Max for unsigned int: 4294967295
#include <stdio.h>#include <limits.h>intmain(){
int n;
unsignedint m;
n = INT_MAX;
m = UINT_MAX;
printf("Out of bounds: %d%d\n",n,n+1);
printf("Out of bounds: %u%u\n",m,m+1);
}
program max_int_limit
implicitnoneinteger :: n
integer :: r,sum!integer*4 :: r,sum!integer*8 :: r,sum!integer*16 :: r,sumsum=0
r =1do n=1,35sum=sum+ r
print*,n,r,sum, sum+1
r = r*2enddoend program max_int_limit