Prime number check acts strange
You need to stop iterating once you know a number isn't prime. Add a break
once you find prime to exit the while loop.
Making only minimal changes to your code to make it work:
a=2
num=13
while num > a :
if num%a==0 & a!=num:
print('not prime')
break
i += 1
else: # loop not exited via break
print('prime')
Your algorithm is equivalent to:
for a in range(a, num):
if a % num == 0:
print('not prime')
break
else: # loop not exited via break
print('prime')
If you throw it into a function you can dispense with break
and for-else:
def is_prime(n):
for i in range(3, n):
if n % i == 0:
return False
return True
Even if you are going to brute-force for prime like this you only need to iterate up to the square root of n
. Also, you can skip testing the even numbers after two.
With these suggestions:
import math
def is_prime(n):
if n % 2 == 0 and n > 2:
return False
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
Note that this code does not properly handle 0
, 1
, and negative numbers.
We make this simpler by using all
with a generator expression to replace the for-loop.
import math
def is_prime(n):
if n % 2 == 0 and n > 2:
return False
return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))
code that check if number is prime number
The range function creates an array of numbers between its parameters.
For example:
>>> range(1, 4)
=> [1, 2, 3]
In your code you're saying a%b
which is finding the remainder between a number the user inputted and a range object. Think of the range object as a python list. So what you're doing is 5 % [1, 2, 3] that doesn't make sense. Check this separate thread to find out how to implement a prime number checker in python. Python Prime number checker
Determine Prime Number in Python DataFrame
I copy and pasted a function to find out if a number is prime from here:
Python Prime number checker
Then I use .apply() to apply this function to every value in column 'Trx':
def isprime(n):
'''check if integer n is a prime'''
# make sure n is a positive integer
n = abs(int(n))
# 0 and 1 are not primes
if n < 2:
return False
# 2 is the only even prime number
if n == 2:
return True
# all other even numbers are not primes
if not n & 1:
return False
# range starts with 3 and only needs to go up
# the square root of n for all odd numbers
for x in range(3, int(n**0.5) + 1, 2):
if n % x == 0:
return False
return True
df['Voucher'] = df['Trx'].apply(isprime)
Resulting dataframe:
Name Email Trx Voucher
0 John john.doe@gmail.com 30 False
1 Sarah sarah@gmail.com 7 True
2 Bob bob@yahoo.com 11 True
3 Chad chad@outlook.com 21 False
4 Karen karen@outlook.com 20 False
5 Dmitri dmitri@rocketmail.com 17 True
Related Topics
Why Doesn't a Python Dict.Update() Return the Object
Problem with Multi Threaded Python App and Socket Connections
Filling in Login Forms in Instagram Using Selenium and Webdriver (Chrome) Python Osx
Timeout for Python Requests.Get Entire Response
Python Pandas Counting the Occurrences of a Specific Value
Ssl.Sslerror: Tlsv1 Alert Protocol Version
How to Properly Assert That an Exception Gets Raised in Pytest
How to Get the Duration of a Video in Python
Calculate Average of Every X Rows in a Table and Create New Table
How to Resolve Typeerror: Can Only Concatenate Str (Not "Int") to Str
Log into Gmail Using Selenium in Python
Validate Ssl Certificates with Python
What Does a for Loop Within a List Do in Python