I'm trying to write a program that ensures to write on a text file finding prime numbers between given number and limit. If the number exists in file, It tries to write another number which is coprime with the given(entered) number. Then it is written on the text file. My problem is checking numbers from text file whether exist or not. How can I write it ? So, I have researched since the morning but, I can't find helpful answers for the problem. I think Python quite a change works from C.
Example: entered number is 12 and limit is 3
generated numbers are 1,5,7
second running 1,5,7 exist on the text file than generates 11,13,17 and print them.
def coprime(x,y):
"""Returns True if the number is copime
else False."""
if x % y == 0:
return False
else:
return True
"""
def text_file() function will be here
if number searching number exists on the text file return True
else return False
"""
f = open("numbers.txt","a+")
i = 0
j = 0
num = int(raw_input("Please enter number "))
limit = int(raw_input("Please enter limit "))
while i < limit:
if text_check(file,j) == False and coprime(num,j) == True:
f.write(str(j))
i += 1
j += 1
print "%d is written on the text file" % j
else:
j += 1
f.close()
a+in quote'a+'. and whats the problem now ?file_write(str(j)). I read that numbers cannot be written directly. They must be converted to string usingstr()? Am I wrong ? @PadraicCunninghamfile, you are redefining a built-in (fileis an alias foropenin Python 2). Personally I find Python much easier and logical than C, and I have been coding in C since the early 1980s.