Python Forum
Problem with very large number for digital root
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with very large number for digital root
#1
Hi,

This is my first post so any help is cool. I'm working with numbers that are 12 to 24 million digits long. What I'm trying to do in Python is run this code.

def digit_root(n): 
    return (n - 1) % 9 + 1 if n else 0
What this does on small numbers is finds its digital root from 1 through 9. So a number like this 8812 equals 1. 1 is the digital root of 8812. However on very large number like a 13 million digit number the shell locks up. Do guys know if this is a code issue, shell issue or hardware issue?

If its code would it be memory and would anyone know a few lines of code to help me? Check this site out it will give you an idea of what I'm doing.

https://www.thonky.com/digital-root-calculator/

[Image: my-pc-hardware.png]
Reply
#2
I was working with big numbers too but I had no printing issue. I use Linux. In my opinion it is pointless to print such a number. It takes time.
Open the task manager and look for memory usage for each process and in general.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
referring to another forum (we don't like to do that), this may be the answer you seek: https://www.daniweb.com/programming/soft...ge-numbers
Reply
#4
(Apr-19-2018, 05:17 AM)Larz60+ Wrote: referring to another forum (we don't like to do that), this may be the answer you seek: https://www.daniweb.com/programming/soft...ge-numbers

Hi Larz60+,

I should clarify, when I past in a number to the shell say 23 million digits long; the shell freezes. Can I configure the memory to the shell and or where is the config file for python shell? Thanks for the link, but I think my numbers might be longer then what they are using.
Reply
#5
I assume you're referring to system shell?
I don't know the answer to that without digging.
Reply
#6
I presume that you are hardcoding the number into the Python interpreter. Instead, put it in a file and read it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(Apr-19-2018, 03:18 PM)wavic Wrote: I presume that you are hardcoding the number into the Python interpreter. Instead, put it in a file and read it.

Thanks wavic,

I'll just have to figure out the code now. Do you have any suggestions for reading a 23milliondigit.txt file into the python interpreter or what code I would use in the shell to initiate it? Thanks :)
Reply
#8
with open('big_num.txt', 'r') as bigggg: # 'r' for read
    num = bigggg.read() # It's string so... next line
    big_n = int(num)
This is how you open a file and read from it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
You can also compute the result incrementally by reading the file by chunks:
KB = 1024
MB = 1024 * 1024

def file_digital_root(file):
    iszero = True
    res = 0
    for s in file:
        n = int(s)
        if iszero and n:
            iszero = False
        res = (res + n % 9) % 9 
    if iszero:
        return 0
    else:
        return res if res else 9

if __name__ == '__main__':
    with open('big_num.txt', buffering=1*MB) as bigggg:
        dr = file_digital_root(bigggg)
    print(dr)
Reply
#10
(Apr-19-2018, 07:35 PM)wavic Wrote:
with open('big_num.txt', 'r') as bigggg: # 'r' for read num = bigggg.read() # It's string so... next line big_n = int(num)
This is how you open a file and read from it.
Hi wavic,
def digit_root(n): 
    return (n - 1) % 9 + 1 if n else 0
with open(digit_root('big_num.txt'), 'r') as bigggg: # 'r' for read
    num = bigggg.read() # It's string so... next line
    big_n = int(num)
Thanks, but I did not know how to integrate it with the digital_root function. I was getting this error " takes 0 positional arguments but 1 was given"

(Apr-19-2018, 08:54 PM)Gribouillis Wrote: You can also compute the result incrementally by reading the file by chunks:
KB = 1024
MB = 1024 * 1024

def file_digital_root(file):
    iszero = True
    res = 0
    for s in file:
        n = int(s)
        if iszero and n:
            iszero = False
        res = (res + n % 9) % 9 
    if iszero:
        return 0
    else:
        return res if res else 9

if __name__ == '__main__':
    with open('big_num.txt', buffering=1*MB) as bigggg:
        dr = file_digital_root(bigggg)
    print(dr)
Gribouillis,

I'm getting somewhere I tested a small number first "124"=7 and it worked. Now I'm crunching the 23 million digit number but its been about 50 minutes. Do you know of any speed enhancements to the code for processing or do I have to wait? I can't wait to see the result lol.
Shocked I'm really happy you helped me on this one I wonder how long it will take anyway have a great day!
Reply


Forum Jump:

User Panel Messages

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