为什么我的计算任意魔方组合数的数学公式在Python中不起作用?

问题描述 投票:0回答:1

N 是立方体的层数。 如果 N 为 2 或 3,则该公式有效。

import math
N = int(input("The number of layers on the cube:\n"))

def factorial(n):
  return math.factorial(n)
#Math
pt1 = factorial(7)*3**6*(24*2**10*factorial(12))**(N%2)*(factorial(24))**math.floor((N-2)/2)*(factorial(24)/factorial(4)**6)**math.floor(((N-2)/2)**2)
#Output

Ans=(int(pt1))
print(f'{Ans:,}')

公式来源

测试公式并比较输出

My code and output

我尝试过chatgpt。 我尝试更改阶乘等数学函数。 我尝试将方程分成几部分。

我需要它按照预期的方式进行计算。

python math syntax combinations rubiks-cube
1个回答
0
投票

问题是通过使用

/
运算符除两个整数,您将得到浮点结果。对于整数除法,请使用
//
运算符。所以而不是:

floor(x/y)

改为使用:

x//y

x
y
是整数时,这将执行整数除法,而不会损失您所看到的精度,从而将商的下限作为整数给出。

© www.soinside.com 2019 - 2024. All rights reserved.