Skip to content

Instantly share code, notes, and snippets.

@chaewonkong
Last active December 7, 2017 07:21
Show Gist options
  • Select an option

  • Save chaewonkong/d87d9d5f8eb4119f872ed84956b3ab2e to your computer and use it in GitHub Desktop.

Select an option

Save chaewonkong/d87d9d5f8eb4119f872ed84956b3ab2e to your computer and use it in GitHub Desktop.
How many prime numbers are there in the given list?
'''Prime number Distinguisher and Counter
In given list, how many prime numbers are there?
Given list = [ 2, 4, 8, 9, 11, 12, 13, 15, 17, 19, 22, 23, 25, 29]
'''
list = [2, 4, 8, 9, 11, 12, 13, 15, 17, 19, 22, 23, 25, 29]
#1. Let's distinguish what prime numbers do we have
prime_list = []
for n in list:
if n == 1:
prime_list.append(n)
else:
num = 0
#make the range narrower
for k in range(1, n//2+1):
if n % k ==0:
num +=1
#Since the prime number can only be divided by 1 and itsef, Since we excluded itself by narrowing the range before,
if num <=1:
prime_list.append(n)
#2. Counting how many prime numbers do we have and print the outcome
print("There are", len(prime_list), "prime numbers in the given list!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment