HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Representation of the formula P(n) = (n!)(6^n)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
representationtheformula

Problem

I'm trying to represent the following mathematical expression in Python:

$$P(n) = (n!)(6^n)$$

The program should compute the answer to expression when n = 156. I have attempted to create the program in python and it produces the correct answer (when compared with wolfram alpha's answer). The answer is approximately \$10^{397}\$.

Is there any way that I could improve the code? (I'm fairly new to programming):

import math
n=156
F = math.factorial(n)
pwr = pow(6,n)
P = F*pwr
print P

Solution

No reason not to make this into a function so it accepts any n.

from math import factorial

def p_of_n(n):
    return factorial(n) * 6**n


Usage:

p_of_n(156)
# output
# 18406764427683965418916585176958885211344286062729194034218082474442355263185795072343804159971536825774135674029142847535155218179997173513194885826708904869034790974161877000771825382336273724208349735310762691458714078255765145189212114617969848727557053656769497332473231995332585780414682079382672173143342192602090273021078801460026695582968493824697958400000000000000000000000000000000000000

Code Snippets

from math import factorial

def p_of_n(n):
    return factorial(n) * 6**n
p_of_n(156)
# output
# 18406764427683965418916585176958885211344286062729194034218082474442355263185795072343804159971536825774135674029142847535155218179997173513194885826708904869034790974161877000771825382336273724208349735310762691458714078255765145189212114617969848727557053656769497332473231995332585780414682079382672173143342192602090273021078801460026695582968493824697958400000000000000000000000000000000000000

Context

StackExchange Code Review Q#71361, answer score: 4

Revisions (0)

No revisions yet.