[Python] Python script to find overlapping prime and happy numbers from files: primer()...

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'm writing a Python script to identify common numbers between a list of prime numbers and a list of happy numbers. I'm generating these numbers and storing them in prime.txt and happy.txt respectively.

    My current implementation involves:

    1. primew(): Writes prime numbers (up to 1000) to prime.txt.
    2. ish(): A helper function to calculate the sum of squares of digits.
    3. ishr(): Writes happy numbers (up to 1000) to happy.txt.
    4. primer() and happyr(): Read and print the contents of the respective files.
    5. overlap(): Intended to read both files, compare numbers, and print any common numbers.

    Here's my initial code (which had issues with function call order and string comparison):

    def primew():
    a=0
    b=open("prime.txt","w")
    while a<1000:
    if a in (2,3,5,7):
    b.write(str(a)+" ")
    elif a%2!=0 and a%3!=0 and a%5!=0 and a%7!=0 :
    b.write(str(a)+" ")
    a+=1

    def primer():
    a=open("prime.txt")
    for b in a:
    print ("Prime numbers are: ")
    print(b)

    def ish(n):
    rem=0
    s=0
    while n>0:
    rem=n%10
    s=s+(rem*rem)
    n=n//10
    return s

    def ishr():
    b=open("happy.txt","w")
    for i in range(1,1000):
    result=i
    while result!=1 and result!=4:
    result=ish(result)
    if result==1:
    b.write(str(i)+" ")

    def happyr():
    bb=open("happy.txt")
    for a in bb:
    print("Happy numbers are: ")
    print(a)

    def overlap():
    b=open("happy.txt")
    c=open("prime.txt")
    for x in b:
    for y in c:
    try:
    if (int(x)==int(y)):
    print("Overlapping are: ")
    print(x)
    except: pass

    happyr()
    primew()
    primer()
    ishr()
    overlap()


    I've attempted to fix some issues by changing the function call order and splitting the lines into individual numbers for comparison. This improved the output for primer() and happyr(), overlap(), but primr() adds some numbers like 121 that is not prime.

    Here is my current fixed code:

    Python

    def primew():
    a=0
    b=open("prime.txt","w")
    while a<1000:
    if a in (2,3,5,7):
    b.write(str(a)+" ")
    elif a%2!=0 and a%3!=0 and a%5!=0 and a%7!=0 :
    b.write(str(a)+" ")
    a+=1

    def primer():
    a=open("prime.txt")
    for b in a:
    print ("Prime numbers are: ")
    print(b)

    def ish(n):
    rem=0
    s=0
    while n>0:
    rem=n%10
    s=s+(rem*rem)
    n=n//10
    return s

    def ishr():
    b=open("happy.txt","w")
    for i in range(1,1000):
    result=i
    while result!=1 and result!=4:
    result=ish(result)
    if result==1:
    b.write(str(i)+" ")

    def happyr():
    bb=open("happy.txt")
    for a in bb:
    print("Happy numbers are: ")
    print(a)

    def overlap():
    b=open("happy.txt")
    c=open("prime.txt")
    for x in b:
    for y in c:
    print(type(x), type(y)) # Debugging line
    y = y.split()
    x = x.split()
    for a in x:
    for b in y:
    try:
    if ((int(a)==int(b))):
    print("Overlapping are: ")
    print((a), (b))
    except Exception as e:
    print("Error:", e)

    primew()
    primer()
    ishr()
    happyr()
    overlap()


    I solved the problem I had this way, The overlap are displayed, but Still I have non-prime numbers written to my file.

    Continue reading...

Compartilhe esta Página