100的阶乘python代码(计算100的阶乘python)
使用Python编程,求1~100所有偶数的阶乘。
total?=?1
for?num?in?range(1,101):
????if?num?%?2?==?0:
????????total?=?total?*?num
print(total)?
#其实一楼答案基本都对了,就是range默认是从0开始的,
#而0对2的求余也是0,所以答案永远是0,设置一个开始值为1,或者2就行了
有汇编大神吗?帮忙写一下100的阶乘的程序
;?8086汇编语言
;?100!=
;?933262154439441526816992388562667004907159682643816214685929
;?638952175999932299156089414639761565182862536979208272237582
;?51185210916864000000000000000000000000???
;?阶乘100!的结果是158位十进制数
;数据段??
data?segment??
??N?EQU?100?????;阶乘?N!
??MAX?EQU?256???;允许阶乘结果的最大位数(十进制数)
??RESULT?DB?MAX?DUP?(0),'$'???;存放阶乘的结果
??TEMP?DB?MAX?DUP?(0)?????????;用于运算的临时缓存??
??N_STR?DB?'!','=',0DH,0AH,'$'
data?ends??????????????????
;代码段
code??segment????
????assume?cs:code,ds:data
start:??
????mov?ax,data
????mov?ds,ax???
???????
????mov?cx,N??????????????;主计数器,N阶乘???
????;================转换数据N为非压缩BCD,存入RESULT?开始
????jcxz?FACT_EXIT
????lea?bx,RESULT+MAX?????;bx指向RESULT末尾字节之后
????mov?ax,cx?
????mov?di,10
BCD_NEXT:
????mov?dx,0
????div?di????????????????;每次都除以10
????dec?bx?
????mov?[bx],dl???????????;将余数(非压缩BCD)存入RESULT
????cmp?ax,0
????jne?BCD_NEXT???
????;================转换数据N为非压缩BCD,存入RESULT?结束
????lea?si,RESULT+MAX??
????sub?si,bx?????????????;si记录BCD串的总长度????
?????
????;================主运算代码?开始
OUTER_LOOP:???????????????;外循环LOOP
????dec?cx????????????????
????cmp?cx,1
????jle?FACT_EXIT?????????
????push?cx???????????????;保护外循环的计数器cx
????;================将RESULT数据拷贝给TEMP缓存?开始
????mov?dx,si
????lea?bx,RESULT+MAX?????;bx指向RESULT末尾字节之后?
????lea?di,TEMP+MAX???????;di指向TEMP末尾字节之后?
COPY_LOOP:?
????dec?bx
????dec?di
????mov?al,[bx]
????mov?[di],al?
????dec?dx
????jnz?COPY_LOOP
????;================将RESULT数据拷贝给TEMP缓存?结束
????dec?cx???????
????cmp?cx,0
????jle?FACT_EXIT????
INNER_LOOP:???????????????;内循环LOOP?
????push?cx???????????????;保护内循环的计数器cx???
????;================用加法代替乘法?开始
????lea?bx,RESULT+MAX?????;bx指向RESULT末尾字节之后?
????lea?di,TEMP+MAX???????;di指向TEMP末尾字节之后?
????mov?cx,si
????clc???????????????????;进位标志清零CF=0?
ADC_LOOP:?????????????????;"带进位加法"循环
????dec?di?
????dec?bx?
????mov?al,[di]???????????;提取TEMP临时字节
????mov?ah,[bx]???????????;提取RESULT字节
????adc?al,ah?????????????;ADC是"带进位加法"
????cmp?al,9
????jna?NOT_CARRY????
????sub?al,10?????????????;保证非压缩BCD的范围是0到9
????stc???????????????????;有进位,CF=1??
????jmp?ADC_GO
NOT_CARRY:??
????clc???????????????????;没有进位,CF=0
ADC_GO:?
????mov?[bx],al???????????;保存加法的结果(非压缩BCD)
????loop?ADC_LOOP?
????jnc?OK_NEXT?????;JNC是检查整个BCD串相加后,最后是否有进位
????inc?si
????dec?bx?
????mov?byte?ptr?[bx],1??;RESULT保存进位CF????
OK_NEXT:?
????;================用加法代替乘法?结束
????pop?cx
????loop?INNER_LOOP??????;内循环LOOP
????
????pop?cx
????jmp?OUTER_LOOP???????;外循环LOOP
????;================主运算代码?结束
????
FACT_EXIT:????
???
????;================屏幕显示阶乘的结果?开始????
????mov?ax,N
????call?SHOW_DEC???;子程序,显示数字N
????lea?dx,N_STR
????mov?ah,9
????int?21H???????;21H中断的9号功能负责在屏幕显示字符串
??
????mov?cx,si?
????lea?bx,RESULT+MAX?
TO_ASCII:?
????dec?bx
????add?byte?ptr?[bx],30H??;将阶乘的结果转换为ASCII
????loop?TO_ASCII??
????
????mov?dx,bx
????mov?ah,9
????int?21H?
????;================屏幕显示阶乘的结果?结束?????
????
????mov?ax,4c00H
????int?21H???
?????
????;================子程序?开始
SHOW_DEC?PROC????;子程序,将16位数转换为ASCII串,在屏幕显示
????push?cx??????;子程序的输入参数是ax
????push?dx??
????push?di
????mov?di,10??????;DIV指令的除数
????mov?cx,0???????;计数器,累计十进制数有多少位数
DIV_NEXT:
????mov?dx,0???????;被除数(dx:ax),只需要ax的16位,将dx设为0
????div?di?????????;除法公式:?(dx:ax)/di?得到?商数ax,余数dx
????push?dx????????;用堆栈暂存获得的余数
????inc?cx
????cmp?ax,0???????;如果除法之后,商数是0,说明已经算完十进制数每位的值,
????jnz?DIV_NEXT???;则退出循环,如果商数不是0,则继续作除法.?
CHAR_NEXT:?????????;十进制数有多少位数就循环多少次
????pop?dx
????add?dl,30H?????;转换为ASCII
????mov?ah,2
????int?21H????????;21H中断的2号功能负责在屏幕显示1个字符
????loop?CHAR_NEXT??
???????
????pop?di
????pop?dx
????pop?cx
????ret
SHOW_DEC?ENDP???
????;================子程序?结束???????????????????????????????
code?ends??
end?start
python怎么求阶乘
解法1
数组解法牛。
首先定义一个ns数组用来存储n!的各个位数上的数值,利用for循环给ns加入10000个0值,以方便后面直接根据index对数组进行操作。
然后定义length作为 “数组的长度”(有真实数值的而非自动添加的0) 也即n!的结果的位数。
之后也必须用到for循环进行累乘,但跟解法一的直接累乘不同,这里是乘数(即i)跟各个位上的数分别相乘,若结果大于等于10则carry0即向前进一位数值为carry,若j循环结束后carry0则说明需要在当前ns的“长度”上进一位,所以length+1即位数+1,这里carry起的就是判断是否进位的作用,而length则代表着结果的位数。
n= int(input())
ns = [0 for i in range(10000) ]
n= int(input())
ns = [0 for i in range(10000) ]
length = 1
ns[0] = length = 1
if n=2:
#for i in range(2,n+1):
##carry = 0
##for j in range(length):
###temp = ns[j] * i + carry
###carry = int(temp/10)
###ns[j] = temp % 10
##while carry0:
###ns[length] += carry%10
###length+=1
###carry = int(carry/10)
while length0:
#length -=1
#print(ns[length],end='')
把# 替换为空格就可以运行。
如输入1000,计算1000!
解法2
print()
m=int(input("计算m!,请输入整数m:"))
import math
a=sum([math.log10(i) for i in range(1,m+1)])
b=int(a)
c=a-b
print(f'{m}!={10**c}*10^{b}')
Python整数阶乘组合计算
#!/usr/bin/env?python3.6
import?re
from?math?import?factorial?as?fac
def?get_k(n,?a):
????nn?=?fac(n)
????for?k?in?range(1,?n):
????????ak?=?a**k
????????if?nn?%?ak?==?0?and?nn?%?(ak?*?k)?!=?0:
????????????return?k
def?main():
????inp?=?input('Enter?two?integers:?')
????n,?a?=?[int(i)?for?i?in?re.findall(r'\d+',?inp)[:2]]
????print(get_k(n,?a))
if?__name__?==?'__main__':
????main()