Python Forum

Full Version: Help with try and open 6.txt file and print as perfect or not
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
As for the f-string question... F-strings are "formatting strings." Yes, it is yet another way to format strings in Python. The benefit of it is that is automatically picks up the value of variable named inside it. For instance:

x = "Hello!"
print(f"{x} How are you?")
will print "Hello! How are you?"
Hi I'm grateful for all your help here everyone.

Am I asking to much, is it possible to find very large perfect numbers with this code we have been working on?

Does it allow for larger perfect numbers?
Have you tried xrange() yet?
(Jan-03-2019, 10:10 PM)nilamo Wrote: [ -> ]Have you tried xrange() yet?
With this number: 191561942608236107294793378084303638130997321548169216
Hi nilamo,

Thanks for the quick response. This is the new error with xrange.

Traceback (most recent call last):
File "C:\Python27\perfect numbers cheker.py", line 24, in <module>
for pair in factor_number(number):
File "C:\Python27\perfect numbers cheker.py", line 16, in factor_number
return [(x, n // x) for x in xrange(1, root + 1) if n % x == 0]
OverflowError: Python int too large to convert to C long

import math

 
KB = 1024
MB = 1024 * 1024
 
def perfect_number(number, factors):
    sum_of_factors = sum(factors) - number
    if sum_of_factors == number:
        return "The number {} is a Perfect number!".format(number)
    else:
        return "The number {} is not a Perfect number!".format(number)
 
def factor_number(n):
    root = int(round(math.sqrt(n), 0))
    return [(x, n // x) for x in xrange(1, root + 1) if n % x == 0]
 
if __name__ == '__main__':
    with open('6.txt', buffering=1*MB) as bigggg:
        for num in bigggg:
            number = int(num)
            factors = []
 
            for pair in factor_number(number):
                factors.extend(pair)
 
            print(perfect_number(number, factors))
 
    factors = []
 
    for pair in factor_number(6):
        factors.extend(pair)
 
    print(perfect_number(6, factors))

nilamo,

2**19-1*(2**18) works

The number 137438691328 is a Perfect number!
Pages: 1 2