This program is used to calculate prime numbers and save them to a file. The saving function hasn't been properly programmed yet so please ignore. The program works by comparing an odd number to previous prime numbers. If it is not a multiple of these numbers then it is prime. In theory it should work however when the I try to divide the number by a prime number from the list it returns the error message:
Traceback (most recent call last): File "C:\Users\Archie\Desktop\maths python\prime\prime v1.3.py", line 51, in primeCheck(num) File "C:\Users\Archie\Desktop\maths python\prime\prime v1.3.py", line 8, in primeCheck check = int(num) / listImport TypeError: unsupported operand type(s) for /: 'int' and 'list'
Can you please either suggest how I can fix this problem or suggest a different approach to the problem.
def primeCheck(num):
divider = 2
refresh = 0
firstPoint = 0
secondPoint = 1
while refresh == 0:
listImport = primeList[firstPoint:secondPoint]
check = int(num) / listImport
if (check.is_integer()):
refresh = 1
else:
firstPoint = firstPoint + 1
secondPoint = secondPoint + 1
if secondPoint > len(primeList):
file.write(str(num) + "\n")
print(str(num))
global x
x = x + 1
refresh = 1
primeList.append
\\ if (int(num)/divider).is_integer():
\\ if divider == num:
\\ file.write(str(num) + "\n")
\\ print(str(num))
\\ global x
\\ x = x + 1
\\ refresh = 1
\\ else:
\\ refresh = 1
\\ else:
\\ divider = divider + 1
global file
repeat = input("How many numbers do you want to add to the existing file?\n")
file = open("Prime results v1.3.txt", "r")
global x
x = 1
num = file.readline()
file.close()
global file
file = open("Prime results v1.3.txt", "a")
num = int(num)
global primeList
primeList = [2]
while x <= int(repeat):
primeCheck(num)
num = num + 2
file.close()
The area double slashed is a previous approach I tried, which worked, however this way is more efficient.
There is a lot of ways how to improve your code. But the reason for the error is that you get a list when you do this primeList[firstPoint:secondPoint]
.
More on this topic is nicely explained here in SO question: Explain Python's slice notation
When you want to index only a single item in a list, you can do it by using my_list[idx]
(note: Python indexes from 0) which returns the item at the position idx
of list my_list
.
It seems to me that the difference between firstPoint
and secondPoint
is always equal to 1 (if I understand your code well). So you dont have to use secondPoint at all.
Just write primeList[firstPoint]
and you get the same result as when you use primeList[firstPoint:secondPoint]
.
There is also an error on the line
primeList.append
Which is a function and not a function call. You probably want to do:
primeList.append(num)
And another tricky part could be, that if you use Python2.x and not Python 3.0 division of two integers is again an integer (e.g. 4/3 = 0
).
So I suggest slight modification:
check = float(num) / listImport
Than 4/3 = 1.3333333333333333
and moreover is_integer()
function raises an error when called on int (and if you use Python 2.x then int/int
returns int
and therefore raises an error.
Example (Python 2.7):
>>> 1/4
0
>>> float(1)/4
1.3333333333333333