answersLogoWhite

0


Best Answer

One way to do this is to write a function that checks if a number is prime:

def isPrime(number):

for i in range(2, number):

if number%i == 0:

return False

return True

Then create variables to track how many primes have been found and a variable to track which number is being tested for being prime. Increment that variable and test it, and if it is prime print it out (or save it somewhere) and increment the variable being used to track how many primes have been found:

targetPrimes = 10 #number of primes to find

primesFound = 0 #number of primes found

i = 1

while (primesFound < targetPrimes):

i += 1 #first number tested is 2

if isPrime(i):

print(i)

primesFound += 1

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program in Python to find the first n prime numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What BASIC program can compute and display all prime numbers from 1 to 40?

PRINT 2,3,5,7,11,13,17,19,23,29,31,37


Why wont this python program print work?

This is the program that I couldn't fit in the question: Sorry for the underscores but it wouldn't let me indent# !/usr/bin/python# Filename: prime5.py#y = int(input('How many prime numbers do you want to see?'))x = 6w = 0primes = [2,3,5]z = len(primes)j = 0while w != 1:____if y*0!=0:________y = input('Please pick another NUMBER: ')____elif y*0==0:________w = w + 1while z


What is the Sum of first n natural numbers?

int sum = 0; int i; for(i = 0; i &lt; n; ++i) { sum += i; }


Python function that lists prime numbers?

If you just want a hint: One way to check whether a number is prime is by dividing it by any number between 2 and the square root of your number. If the number divides by any of these, it is not prime. If you want the code: import math for num in range(1,101): if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)): print num


How do you write a program which calculating LCM of 2 numbers in Linux?

Dunno about Linux, but I've written mine in C.It prime factorises the numbers, making a note of the highest power of each prime factor as it goes (in a linked list of malloc()ed structures). Once all the numbers have been factorised, it has a list of all the primes used along with their highest power. The lcm is then the product of the primes raised to their highest power.You are also not limited to the lcm of 2 numbers - you can keep factorising numbers until you run out of them and find the lcm of them all!Whilst you're at it you can add finding their hcf very easily: this time it's the product of the common primes to their lowest power.All that is then needed is the prime factorisation of the numbers.The normal method is:try the first prime (2)If it does not divide the number: set the prime to the next primeTry again from step 2Add one to the power count of this primereplace the number by the number divided by the primeif the number is not 1 go back to step 2Found all primes, stop!Finding the primes by which to divide is not easy on the fly, so you could check 2 specifically and then all odd numbers 3, 5, 7,..., but an improvement is to specifically check 2 and 3 and then check the numbers 6n &plusmn; 1 (which may be prime and why are 6n, 6n &plusmn; 2 and 6n &plusmn; 3 definitely not prime?) which skips every third odd number - this sequence of potential primes (5, 7, 11, 13, 17, 19, ...) can be easily generated.And while you're at it, you could display the prime factorisation you've done.And using that prime factorisation you can list the factors (and factor pairs) for the numbers.Obviously you'll need to sort out how the numbers are input to the program - I decode argv[], but you could use reading from stdin if you prefer.

Related questions

How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


How do you write a VBnet program to find the prime numbers between 100 to 200?

VBnet program to find the prime numbers between 100 to 200?


Write a program to print first 100 alternative prime numbers?

This would require some computer knowledge. It can make it easier to find out the prime numbers without figuring it out in your head.


Program for print prime all number from 1 to 100 in foxpro?

Prime numbers are numbers that are only divisible by themselves and the number 1. You can write a program to print all prime numbers from 1 to 100 in FoxPro.


What are the first three prime numbers?

The first three prime numbers are 2,3 and 5.


What is the sum of the first 15 prime numbers?

the sum of the first 15 prime numbers is 328 .


What will be the sum of the first 25 prime numbers?

The sum of the first 25 prime numbers is 1,060.


What is the sum of the first 250 prime numbers?

The sum of the first 250 prime numbers is 182,109.


What are the first twenty six prime numbers?

The first 26 prime numbers are :- 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97and 101


How do you write a program in objective c numbers 1-100 prime numbers?

fdsgfhgdfhgdf


What is the arithmetic mean of the sum of the first five prime numbers and the sum of the cubes of the first three prime numbers?

The sum of the first five prime numbers is 28. The sum of the cubes of the first three prime numbers is 160. The average of 28 and 160 is 94.


Write a C program to find the sum of all prime numbers?

Since there is an infinite set of prime numbers the answer would be infinity.