Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prime Numbers
#1
import math
prime_count = 0
#This Code checks if the n integer is prime!
def is_prime(n):
    if n % 2 == 0 and n > 2: # 2 is the only even prime
            return False
    return all(n% i for i in range(3, int(math.sqrt(n)) + 1, 2))

print(is_prime(5))

import sys
prime_count = 0 #prime counter
for arg in sys.argv[1:]: #for all the arguments (typed) in the command
    n = int(arg)
    if (is_prime(n)):
        print('The Number ',n,' is Prime!') #Displays the prime number if True
        prime_count += n #prime_count = primecount + 1

print('Total Primes:', prime_count)
I understand the whole code just there's this bit that I don't understand:
return all(n% i for i in range(3, int(math.sqrt(n)) + 1, 2))
And it didn't work any other way it would start giving me 4 and 8 as prime numbers Think

Anyone care to explain?
Reply
#2
First you get the square root of n (math.sqrt(n)). If n has any factors other than itself and 1 (that is, if n is composite/non-prime), one of those factors will be less than it's square root. You convert that float to an int, because range only takes ints. This lowers the value, so you add one to make sure the square root is included.

Now, you've already checked for two being a factor, which counts as checking all the even factors. So you get the range from 3, to square root, taking every second number. So 3, 5, 7, ... This gives you all the odd factors. You loop through those odd factors with a comprehension statement (n % i for i in range(...)), getting n % i for each odd factor i. Since n % i is non-zero if i is not a factor of n, and non-zeros evaluates as True, all() returns True if none of the odd factors are actually factors of n. (all returns True if all of the items in the sequence passed to it are True, and False if any of them are false.)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  prime numbers with iterator and generator cametan_001 8 1,874 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  prime numbers astral_travel 28 3,663 Nov-08-2022, 09:23 PM
Last Post: astral_travel
  Return prime numbers from range krzyfigh 2 1,929 Apr-20-2020, 08:08 PM
Last Post: krzyfigh
  Prime numbers Anderi02 1 1,978 Oct-13-2019, 04:49 PM
Last Post: ichabod801
  first k non-prime numbers arycloud 11 7,268 Jul-09-2019, 02:19 PM
Last Post: abhi19935
  first k non prime numbers print bsrohith 7 7,540 Jun-20-2019, 10:48 AM
Last Post: arycloud
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,741 May-09-2019, 12:19 PM
Last Post: Pleiades
  Finding prime numbers creslin_black 7 4,393 Jul-20-2018, 02:28 PM
Last Post: grjmmr
  prime numbers generator is generating non prime numbers? ixaM 2 4,467 Dec-18-2016, 01:35 PM
Last Post: ixaM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020