Python Forum
Help with try and open 6.txt file and print as perfect or not
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with try and open 6.txt file and print as perfect or not
#11
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?"
Reply
#12
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?
Reply
#13
Have you tried xrange() yet?
Reply
#14
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 281 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 946 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Are there errors in the code for my coin toss systems? Matlibplot is too perfect . Coolkat 0 342 Nov-13-2023, 11:54 AM
Last Post: Coolkat
  How can i combine these two functions so i only open the file once? cubangt 4 805 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Start print a text after open an async task via button Nietzsche 0 673 May-15-2023, 06:52 AM
Last Post: Nietzsche
  I cannot able open a file in python ? ted 5 3,051 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,309 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  Saving the print result in a text file Calli 8 1,700 Sep-25-2022, 06:38 PM
Last Post: snippsat
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,362 May-06-2022, 01:44 PM
Last Post: Larz60+
  failing to print not matched lines from second file tester_V 14 5,949 Apr-05-2022, 11:56 AM
Last Post: codinglearner

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020