Nodachisoft Nodachi Sword Icon
  
@Amaji✎ Update October-16-2020

Generate an Array of Prime numbers in python

Here's a code snippet for a program that counts primes in the range you specify in python.

When you want to count primes like in the image, or when you want to count primes to calm yourself down, you'll want to make sure the prime numbers are correct (?).

This is a formatted code that I wrote while solving a little puzzle (CTF).

I wrote it in a reusable form as a function, so feel free to modify it if you want.

I do not use any libraries such as Numpy in python.

Tested Environment

  • python 2.7.15、3.8.5

Code

get_primelist is a function that counts prime numbers from 2 to upper.

It returns an array of prime numbers from 2 to upper.

 
get_primelist.py
def get_primelist(upper):
	result = []
	for cp in range ( 2, upper + 1 ):
		for i in range ( 2, cp ):
			if ( cp % i == 0 ):
				break
		else:
			result.append(cp)
	return result

# RUN to create an array of the prime numbers
# [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
#  43,47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ]
print (get_primelist(100))

Even with a very simple program, you can quickly calculate the range from 2 to 100,000, so if you don't want speed, you don't have to tune it as hard as you would like.

For further information: Processing time, etc.

The following table shows the estimated time for the calculation within a practical time.

On Linux, it is the time to display the result of the command time.

Width of primes to calculate time (average of 3 runs)
1~100 0.03 sec
1~1,000 0.03 sec
1~10,000 0.29 sec
1~100,000 22.55 sec

Measured environment

  • Python : python3.8.2
  • OS : Ubuntu 20.04 LTE @ Windows10 Home x64 WSL2
  • Processor : Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
  • RAM : 8.00 GB

I think you can get more complete and quicker prime numbers if you use a proper math library or something. I may add articles using the library in the future.

speed-up memo

I'd like to add a slightly improved version, like sifting eratosthenes and cutting branches, if I have time.

The end.

References

Update

  • October-16-2020 First Edition
 
 
Message sent

Thank you for your message.

Something error has occured!

Sorry. The Error has occurred.We apologize for the inconvenience.Please try again in a few minutes or contact us via DM below.

Twitter:@NodachiSoft_eng
Name:
 
Replay To:
 
Message:
 
Back
Check the content!

Send the following information to us. If you are happy with your submission, please click "Send". If you want to modify it, please click "Back".

Name:
 
Reply To:
 
Message:
 
Enter a confirmation key to make sure that you are not operating from a Robot.
Confirmation Key is 95
Back
 / 
Go to Confirmation
Entry fields
Go to Confirmation

There are 1 articles that may be relevant!

Move and copy files to another folder using Python

#Python#Code snipet✎ 2020-09-29
A program that uses Python to move files to another folder
広告領域
追従 広告領域
Table Of Contents
Generate an Array of Prime numbers in python
Generate an Array of Prime numbers in python
Tested Environment
Tested Environment
Code
Code
RUN to create an array of the prime numbers
RUN to create an array of the prime numbers
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43,47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ]
43,47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ]
For further information: Processing time, etc.
For further information: Processing time, etc.
Measured environment
Measured environment
speed-up memo
speed-up memo
References
References
Update
Update
Nodachisoft © 2020