Python Forum
how do I show 12 numbers from a huge number?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do I show 12 numbers from a huge number?
#21
(Sep-03-2019, 01:36 AM)jefsummers Wrote: I don't think finding on a site is the point. If the prof wants you to show your work showing a web search will get you a worse grade then answering in base 2.

What is this project aiming to teach? That is where you need to go.

No matter the source, the teacher just wants the first 12 digits and the last 12 digits. He was explaining to us how many bytes are generated per day. (or per day, or per year, I don't remember exactly)
Reply
#22
anabeatriz Wrote:No matter the source, the teacher just wants the first 12 digits and the last 12 digits.
Are you sure he is a teacher? You could hire a freelance programmer to do the job, your teacher would probably be delighted! All this is not serious.
Reply
#23
(Sep-03-2019, 04:06 PM)Gribouillis Wrote:
anabeatriz Wrote:No matter the source, the teacher just wants the first 12 digits and the last 12 digits.
Are you sure he is a teacher? You could hire a freelance programmer to do the job, your teacher would probably be delighted! All this is not serious.

calm down, it's just a challenge! Wink
Reply
#24
As an aside, although modular math and abstract algebra can be useful for this problem, the difficulties he is having suggests this is a 100 level course. When I took abstract algebra, back in the paleozoic age, it was a 300 level course. I would not expect the 100 level course to have a 300 as a prerequisite, so the teacher is likely thinking about other solutions.

At this point I am in the postgraduate education biz. When I am teaching (not anything related to math anymore) I am recommending the student look at the problem from the point of view of "what is my instructor trying to get me to learn from this". Having extra info is always nice, but not necessarily what is being looked for.

When I took assembler we were assigned to create a function that would return successive numbers in the fibonacci sequence. Extra credit for doing it in 10 instructions or less. I did it in 4, but used some special knowledge. Prof was impressed but did not recommend that approach to the rest of the class.

Just sayin'
Reply
#25
Now I just need a way to find the first 12 digits! (The first 12 digits I found on a website are wrong) Please help me.
Reply
#26
I have to look in my Math-book. Maybe there is a solution: http://weitz.de/KMFI/
The book is good, but it's only in German. He uses Python to explain math.

With modular arithmetic you get the last x digits of the solution.
The built-in function pow supports it.

The equation is: 2 ** (2 ** 34)
pow(2, 2**34, 10**12)
This should calculate the last 12 digits of 2 ** (2**34)



The other solution to get the first x digits can may be done with iteration.
def ipow(x, y, digits):
    for _ in range(y - 1):
        x *= x
        # do something to truncate x?
        # maybe working with math.log10
By the way, this question is very old: https://stackoverflow.com/questions/6351...e-required

Another solution is the usage of math.log10: https://stackoverflow.com/questions/3873...nentiation

def get_first_digits(a, b, n):
    x = b * math.log10(a)
    y = math.floor(pow(10, x - math.floor(x) + n - 1))
    return int(y)
Output:
In [65]: get_first_digits(2,2**34,12) Out[65]: 927436539881
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#27
BTW - for "fun" I did my method of treating each digit as an element in a list, ran it in IDLE, Python 3.7, on a core i7 3.4GHz 16gig machine over the weekend. In 72 hours it got to about the 3.5 millionth power. Unfortunately it needs to get to the 17 billionth. Since by necessity it runs progressively slower (more digits to multiply) this method would not be done by December!
Again for fun I'm running overnight using iPython and numpy to see if that speeds things up, same computer.
J
Reply
#28
(Sep-10-2019, 11:22 AM)jefsummers Wrote: BTW - for "fun" I did my method of treating each digit as an element in a list, ran it in IDLE, Python 3.7, on a core i7 3.4GHz 16gig machine over the weekend. In 72 hours it got to about the 3.5 millionth power. Unfortunately it needs to get to the 17 billionth. Since by necessity it runs progressively slower (more digits to multiply) this method would not be done by December!
Again for fun I'm running overnight using iPython and numpy to see if that speeds things up, same computer.
J
What code did you use for this?
Reply
#29
This is the homework board so can't give you the code, but if you look back a few posts I gave you the layout of how I would write it. It was kind of like elementary school math - multiply the first digit/list element by 2, if greater than 10 subtract 10 and carry the 1. Proceed through all the digits. Oh, and important to test with a shorter loop, like calculating 2**16 before tackling 2**(2**34).
Reply
#30
(Sep-10-2019, 07:20 AM)DeaD_EyE Wrote: I have to look in my Math-book. Maybe there is a solution: http://weitz.de/KMFI/ The book is good, but it's only in German. He uses Python to explain math.
With modular arithmetic you get the last x digits of the solution. The built-in function pow supports it. The equation is: 2 ** (2 ** 34)
pow(2, 2**34, 10**12)
This should calculate the last 12 digits of 2 ** (2**34)
The other solution to get the first x digits can may be done with iteration.
 def ipow(x, y, digits): for _ in range(y - 1): x *= x # do something to truncate x? # maybe working with math.log10 
By the way, this question is very old: https://stackoverflow.com/questions/6351...e-required Another solution is the usage of math.log10: https://stackoverflow.com/questions/3873...nentiation
 def get_first_digits(a, b, n): x = b * math.log10(a) y = math.floor(pow(10, x - math.floor(x) + n - 1)) return int(y) 
Output:
In [65]: get_first_digits(2,2**34,12) Out[65]: 927436539881

Thank you, I will show 927436539881 to my teacher and then you will know his answer
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,428 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 3,022 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Divide a number - Multiple levels - Sum of all numbers equal to input number pythoneer 17 8,835 Apr-20-2018, 04:07 AM
Last Post: pythoneer
  Trying to get the week number from a string of numbers fad3r 2 3,213 Apr-15-2018, 06:52 PM
Last Post: ljmetzger
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,115 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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