Python Forum

Full Version: how do I show 12 numbers from a huge number?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
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.
What have you done so far?
Likely you will use / (division) and % (mod)
(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?
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
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.
(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?
(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.
(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?
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?
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
>>> 
Pages: 1 2 3 4