how many does the factorial of n(in binary data ) have zero?

本文深入分析了如何计算给定数值的阶乘在二进制表示下拥有多少个零,通过算法实现并提供具体实例。

how many does the factorial of n(in binary data ) have zero?

个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;

博客内容:how many does the factorial of n have zero?

博客时间:2014-5-6;

编程语言:Java ;

编程坏境:Windows 7 专业版 x64;

编程工具:jdk,eclipse x64;

制图工具:office 2010 ppt;

硬件信息:7G-3 笔记本;


my words

Attitude beats all.

problem( from beauty of programming )

how many does the factorial of n(in binary data ) have zero?

eg: n = 5;

n! = 1 * 2 * 3 * 4 * 5  =  120 =  0111 1000 ;

the right result is 3.

my solution 

as for n!, the time that n! can divided by 2 without reminder is the result.


experiments


program note:

for every row 

first number: n

second number: the result the problem require

my code

test.java

package test;


public class test {

	public static void main(String[] args) {
		int i =0;
		while(i < 100)
			System.out.println(i+" "+_zero_number_with_binary(i++));

	}

	static int _zero_number_with_binary(int n)
	{
		int result = 0;
		int i = n ;
		while(i>0)
		{
			result += i/2 ;
			i = i/2 ;
		}
		return result;
	}

}


你遇到的错误: ``` factorial n=factorial(n) 1 Error: This name does not have a type, and must have an explicit type. [FACTORIAL] ``` 是 **Fortran 编译器** 报错,意思是:**你调用了一个名为 `factorial` 的函数,但编译器不知道这个函数的返回类型**。 --- ## 🔍 错误原因分析: 在 Fortran 中,**如果你在子程序或函数中调用另一个函数,而没有显式声明它的返回类型**,编译器会根据 **首字母规则(implicit typing rules)** 来猜测它的类型,例如: - 以 `i`, `j`, `k`, `l`, `m`, `n` 开头的变量默认是 `integer` - 其它字母开头的默认是 `real` 所以编译器看到你调用 `factorial(n)` 时,发现你没有声明 `factorial` 的类型,就默认它是 `integer` 类型,但你实际定义的 `factorial` 函数返回的是 `real` 类型,这就导致了类型冲突。 --- ## ✅ 解决方法:**显式声明函数返回类型** 你可以在调用 `factorial` 函数的子程序中,**显式声明它的返回类型为 `real`**。 --- ### ✅ 修改后的完整代码如下: ```fortran program permutation_combination implicit none integer :: n, m real :: perm, comb n = 12 m = 8 call compute_perm_comb(n, m, perm, comb) print *, "排列数 P(", n, ",", m, ") = ", perm print *, "组合数 C(", n, ",", m, ") = ", comb end program permutation_combination ! 子程序:计算排列数和组合数 subroutine compute_perm_comb(n, m, perm, comb) implicit none integer, intent(in) :: n, m real, intent(out) :: perm, comb real :: factorial_n, factorial_m, factorial_nm real :: factorial ! <------ 显式声明 factorial 函数返回类型为 real factorial_n = factorial(n) factorial_m = factorial(m) factorial_nm = factorial(n - m) perm = factorial_n / factorial_nm comb = factorial_n / (factorial_m * factorial_nm) end subroutine compute_perm_comb ! 阶乘函数(使用递归) real function factorial(x) implicit none integer, intent(in) :: x if (x == 0 .or. x == 1) then factorial = 1.0 else factorial = x * factorial(x - 1) end if end function factorial ``` --- ### ✅ 关键修改点: ```fortran real :: factorial ``` 在 `subroutine compute_perm_comb` 中添加这一行,告诉编译器 `factorial` 是一个返回 `real` 类型的函数。 --- ### ✅ 输出结果(运行结果不变): ``` 排列数 P( 12, 8 ) = 19958400.0 组合数 C( 12, 8 ) = 495.00000 ``` --- ### ❗ 可选优化建议: 如果你希望更严谨,可以将 `factorial` 函数提取为 `module` 中,这样可以自动声明接口,避免手动声明函数类型。 --- ## ✅ 使用 module 的更规范写法: ```fortran module math_utils implicit none contains real function factorial(x) integer, intent(in) :: x if (x == 0 .or. x == 1) then factorial = 1.0 else factorial = x * factorial(x - 1) end if end function factorial end module math_utils ! ================================== program permutation_combination use math_utils implicit none integer :: n, m real :: perm, comb n = 12 m = 8 call compute_perm_comb(n, m, perm, comb) print *, "排列数 P(", n, ",", m, ") = ", perm print *, "组合数 C(", n, ",", m, ") = ", comb end program permutation_combination ! 子程序:计算排列数和组合数 subroutine compute_perm_comb(n, m, perm, comb) implicit none integer, intent(in) :: n, m real, intent(out) :: perm, comb real :: factorial_n, factorial_m, factorial_nm factorial_n = factorial(n) factorial_m = factorial(m) factorial_nm = factorial(n - m) perm = factorial_n / factorial_nm comb = factorial_n / (factorial_m * factorial_nm) end subroutine compute_perm_comb ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值