1

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()
13
  • You need to put a+ in quote 'a+'. and whats the problem now ? Commented Dec 30, 2014 at 12:58
  • how are you writing the numbers to file? Commented Dec 30, 2014 at 12:59
  • using file_write(str(j)). I read that numbers cannot be written directly. They must be converted to string using str() ? Am I wrong ? @PadraicCunningham Commented Dec 30, 2014 at 13:01
  • 1
    @PadraicCunningham Not to make fun But check this tweet Commented Dec 30, 2014 at 13:04
  • 1
    Bad idea to use the name file, you are redefining a built-in (file is an alias for open in Python 2). Personally I find Python much easier and logical than C, and I have been coding in C since the early 1980s. Commented Dec 30, 2014 at 13:15

1 Answer 1

1

presuming all nums are all on separate lines like below:

1
5
7



from fractions import gcd

def coprime(n1, n2):
    return gcd(n1, n2) == 1 # same as if gcd(n1, n2) == 1:return True else: return False



with open("out.txt","a+") as f: # with automatically closes your files
    # add all previous numbers into a set, casting to int using map 
    # map(int, f)  equivalent to [int(ele) for ele in f] in python2
    nums_set = set(map(int, f)) # with 1 5 and 7 in the file nums_set = set([1, 5, 7])
    i = 0
    j = 0

    num = int(raw_input("Please enter number "))
    limit = int(raw_input("Please enter limit "))

    while i < limit:
        if j not in nums_set and coprime(num,j):
            f.write("{}\n".format(j))
            print "{} is written on the text file.".format(j)
            i += 1
            j += 1
            # add current j's from this run to the set to avoid reopening and rechecking he file        
            nums_set.add(j)       
        else:
            j += 1
Sign up to request clarification or add additional context in comments.

2 Comments

thank you. But, I want to learn map ? I have some information about set now.
map(int,f) is equivalent to [int(ele) for ele in f]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.