Python Forum
how do I show 12 numbers from a huge number? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: how do I show 12 numbers from a huge number? (/thread-20815.html)

Pages: 1 2 3 4


how do I show 12 numbers from a huge number? - anabeatriz - Aug-31-2019

I want the answer of this expression using python: 2 ** 2 ** 34 (two raised to two that is raised to thirty-four) but I only need the first 12 and last 12 numbers of the result of this expression. How can I solve this problem? It is a homework. The deadline is until December this year.


RE: how do I show 12 numbers from a huge number? - jefsummers - Sep-01-2019

What have you done so far?
Likely you will use / (division) and % (mod)


RE: how do I show 12 numbers from a huge number? - anabeatriz - Sep-01-2019

(Sep-01-2019, 12:14 AM)jefsummers Wrote: What have you done so far?
Likely you will use / (division) and % (mod)


I already ran two codes:
n = 2
for c in range (2, 34+2):
   print("\n", n)
   n = n * n
The code above will show all thirty-four terms. But when I execute it, Pycharm crashes

and

n = str(2 ** 2 ** 34)
print(str(n[:12]))
The code above will show the last number (which is in the thirty-fourth term) but only the first 12 digits will appear on the screen. When I run it, Pycharm crashes

should I run them on a computer that has more memory?


RE: how do I show 12 numbers from a huge number? - jefsummers - Sep-01-2019

So I understand the problem, is it (2**2)**34 or 2**(2**34)? The way written it would be the former which would be 4**34 as a first step


RE: how do I show 12 numbers from a huge number? - ichabod801 - Sep-01-2019

If it was as easy as calculating the number and converting it to a string, I expect your professor would not have given you until December to solve it. Especially not if it's crashing Pycharm. Note that if you only care about the last 12 digits, you only need to keep the last 12 digits. You could keep trimming off the extra digits, and use a loop to calculate the right number of multiplications. You will probably need to break down the number to keep the number of loops you need to do down.

The higher end would be trickier, since the lower digits still multiply and can thus bleed up into the higher digits. I would look at the higher powers of 2 that you can calculate and see if there are any patterns you can use to solve the problem.


RE: how do I show 12 numbers from a huge number? - anabeatriz - Sep-01-2019

(Sep-01-2019, 02:15 AM)jefsummers Wrote: So I understand the problem, is it (2**2)**34 or 2**(2**34)? The way written it would be the former which would be 4**34 as a first step

It's 2**(2**34)

(Sep-01-2019, 03:41 AM)ichabod801 Wrote: If it was as easy as calculating the number and converting it to a string, I expect your professor would not have given you until December to solve it. Especially not if it's crashing Pycharm. Note that if you only care about the last 12 digits, you only need to keep the last 12 digits. You could keep trimming off the extra digits, and use a loop to calculate the right number of multiplications. You will probably need to break down the number to keep the number of loops you need to do down.

The higher end would be trickier, since the lower digits still multiply and can thus bleed up into the higher digits. I would look at the higher powers of 2 that you can calculate and see if there are any patterns you can use to solve the problem.

I need the first 12 digits and the last 12 digits. How would you solve this problem with code?


RE: how do I show 12 numbers from a huge number? - ichabod801 - Sep-01-2019

(Sep-01-2019, 12:56 PM)anabeatriz Wrote: How would you solve this problem with code?

We're not going to write your code for you. We'd be happy to help you fix problems with your code, but before we can do that, you need to write some code.

I'm not even sure what the solution is, I just posted the direction I would take.


RE: how do I show 12 numbers from a huge number? - anabeatriz - Sep-01-2019

(Sep-01-2019, 01:09 PM)ichabod801 Wrote:
(Sep-01-2019, 12:56 PM)anabeatriz Wrote: How would you solve this problem with code?

We're not going to write your code for you. We'd be happy to help you fix problems with your code, but before we can do that, you need to write some code.

I'm not even sure what the solution is, I just posted the direction I would take.


I already ran two codes:
n = 2
for c in range (2, 34+2):
   print("\n", n)
   n = n * n
and

n = str(2 ** 2 ** 34)
print(str(n[:12]))
but both take a long time to show the answer. Should I run them on a computer that has more memory?


RE: how do I show 12 numbers from a huge number? - perfringo - Sep-02-2019

At the beginning there is "2 ** 2 ** 34 (two raised to two that is raised to thirty-four)" but later of it's changed to "It's 2**(2**34)". Which is correct?


RE: how do I show 12 numbers from a huge number? - buran - Sep-02-2019

actually the result of 2**2**34, (2**2)**34 and 2**(2**34) is the same
ups Rolleyes Blush

>>> 2**2**3
256
>>> (2**2)**3
64
>>> 2**(2**3)
256
>>>