First prime number in python assignment expert

Answer to Question #245119 in Python for Raju

First Prime Number

You are given N inputs. Write a program to print the first prime number in the given inputs.

n = int(input("Enter number of integers: ")) numbers = [] for i in range(n):     number = int(input("Enter number: "))     isPrime=True     if number > 1:         for j in range(2, number):             if (number % j) == 0:                 isPrime=False                 break         if isPrime==True:             numbers.append(number)                  print("The first prime number in the given inputs is: "+str(numbers[0]))

Learn more about our help with Assignments: Python

First Prime Number

You are given N inputs. Write a program to print the first prime number in the given inputs.

Input

The first line of input is an integer N. The next N lines each contain an integer. Explanation

In the given example of

5 integers

1

10

4

3

2

The output should be

3.

Sample Input 1

5

1

10

4

3

2

Sample Output 1

3

Sample Input 2

4

2

3

5

7

Sample Output 2

2

n = int(input("Enter a positive integer: ")) is_primes = []  # list of potential prime numbers(contains primes and non primes) for count in range(n):     read = int(input("> "))     is_primes.append(read) non_primes = [] for num in is_primes:     if num == 2 or num == 3:         pass     else:         for i in range(2, num):             if num % i == 0:                 non_primes.append(num)                 break             else:                 pass              print(sum(non_primes))

Learn more about our help with Assignments: Python

For reference, there's a pretty significant speed difference between the various stated solutions. Here is some comparison code. The solution pointed to by Lennart is called "historic", the one proposed by Ants is called "naive", and the one by RC is called "regexp."

from sys import argv from time import time def prime(i, primes): for prime in primes: if not (i == prime or i % prime): return False primes.add(i) return i def historic(n): primes = set([2]) i, p = 2, 0 while True: if prime(i, primes): p += 1 if p == n: return primes i += 1 def naive(n): from itertools import count, islice primes = (n for n in count(2) if all(n % d for d in range(2, n))) return islice(primes, 0, n) def isPrime(n): import re # see //tinyurl.com/3dbhjv return re.match(r'^1?$|^(11+?)\1+$', '1' * n) == None def regexp(n): import sys N = int(sys.argv[1]) # number of primes wanted (from command-line) M = 100 # upper-bound of search space l = list() # result list while len(l) < N: l += filter(isPrime, range(M - 100, M)) # append prime element of [M - 100, M] to l M += 100 # increment upper-bound return l[:N] # print result list limited to N elements def dotime(func, n): print func.__name__ start = time() print sorted(list(func(n))) print 'Time in seconds: ' + str(time() - start) if __name__ == "__main__": for func in naive, historic, regexp: dotime(func, int(argv[1]))

The output of this on my machine for n = 100 is:

naive [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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541] Time in seconds: 0.0219371318817 historic [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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541] Time in seconds: 0.00515413284302 regexp [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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541] Time in seconds: 0.0733318328857

As you can see, there's a pretty big discrepancy. Here it is again for 1000 (prime outputs removed):

naive Time in seconds: 1.49018788338 historic Time in seconds: 0.148319005966 regexp Time in seconds: 29.2350409031

How do I find the first prime number in Python?

1 Answer.
numr=int(input("Enter range:")).
print("Prime numbers:",end=' ').
for n in range(1,numr):.
for i in range(2,n):.
if(n%i==0):.
break..
print(n,end=' ').

How do you print the first prime number in inputs Python?

Question and sample input : number_of_digits = int(input()) prime = 0 count = 0 for num in range(1, number_of_digits): given_input = int(input()) for b in range(2, given_input): if (given_input % b == 0): count += 1 else: prime = prime + given_input break print(prime) ``` what is the error in my code!?

What is the code for prime number in Python?

from math import sqrt # Number to be checked for prime n = 9 flag = 0 if(n > 1): for k in range(2, int(sqrt(n)) + 1): if (n % k == 0): flag = 1 break if (flag == 0): print(n," is a Prime Number! ") else: print(n," is Not a Prime Number! ") else: print(n," is Not a Prime Number!

What is first prime number?

The first five prime numbers: 2, 3, 5, 7 and 11. A prime number is an integer, or whole number, that has only two factors — 1 and itself. Put another way, a prime number can be divided evenly only by 1 and by itself. Prime numbers also must be greater than 1.

Chủ đề