最大阶乘数作为某个整数 n 的因子出现

发布于 2024-11-18 04:08:11 字数 283 浏览 10 评论 0原文

我正在设计一种算法来查找作为某个整数 n 中的因子存在的最大阶乘数。这个问题在RGDormey的《如何用计算机解决它》中给出。 你能帮助我如何设计算法吗?答案必须是整数 n 的因数,也是阶乘数。

我想到的解决方案:

首先确认整数不是质数。如果是质数,则没有进一步的解决方案。

如果不是质数,找出整数的最大因子

检查它是否是阶乘数。

如果是,那就是答案

如果不是,找出整数的第二大因子..

检查它是否是阶乘数...

等等..

I am working on designing an algorithm to find the largest factorial number present as a factor in some integer n. This problem is given in "How to solve it by computer" by R.G.Dormey.
could you help me in how to go about designing algorithm.. answer has to be a factor of the integer n and also a factorial number..

solution I thought of:

first confirm that integer is not prime. if prime, no further solution posible..

if not prime, find out the largest factor of the integer

check if it is a factorial number or not..

if yes, that is the answer

if not, find out second largest factor of the integer..

check if it is a factorial number or not...

and so on..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

过期情话 2024-11-25 04:08:11

将 N 除以 1,然后将结果除以 2,然后将结果除以 3,...然后除以 k+1。一旦 k+1 没有整数结果,k!是一个答案。

Divide N by 1, then the result by 2, then the result by 3,... then by k+1. Once there is no integer result for k+1, k! is an answer.

千秋岁 2024-11-25 04:08:11

您正在寻找整数中最大的阶乘,因此您只需要稳步增加因子即可。即检查结果整数是否能被 2、3、4 等整除...第一次失败时,您知道没有更大的阶乘可以整除您的整数。例如,如果您的整数可以被 2 ... 6 整除,但不能被 7 整除,那么您就知道答案是 6!。

You're looking to find the largest factorial that goes into your integer, so you just need to steadily increase your factors. I.e. Check to see if the resulting integer is divisible by 2, 3, 4, etc... The first time you get a failure you know that no larger factorial will divide your integer. E.g. if your integer is divisible by 2 ... 6 but not 7, you know the answer is 6!.

落日海湾 2024-11-25 04:08:11

考虑到阶乘的特性会变得非常非常大非常快...我很想尝试直接除以它。
首先找到小于您的数字的最大阶乘...然后开始通过除法进行检查
例如:设 N 为您的号码。设阶乘 f(fac,num) 为阶乘函数,它返回的阶乘小于您的数字。这样 num! = 面
然后执行以下操作:

检查 if (N % fac)==0;否则尝试 (N*num) % fac 然后 (N* (num)*(num-1)) % fac

你需要的答案是 (num) 在第一种情况下 (num-1) 在第二种情况下,依此类推

given the property of factorials to grow very very large very fast...i would be tempted to try dividing it directly.
first find the largest factorial less than your number...then start checking by dividing
eg: let N be your number. Let factorial f(fac,num) be your factorial function which returns a factorial less than your number. such that num! = fac
Then do the following:

Check if (N % fac)==0; else try (N*num) % fac then (N* (num)*(num-1)) % fac

the answer u require is (num) in first case (num-1) in second case and so on

岁月静好 2024-11-25 04:08:11
long prod = 1;
long maxFactor = 1;

for(long i=2; i<=n && prod< n && n%i==0 ;i++){

    prod  = prod*i;
    if(n%prod == 0) maxFactor = prod;
}

这里n是我们需要找出最大阶乘因子的数字,macFactor的最终值就是最终的解。

注意:一个数的因数的因数也是该数的因数。

如果因子是阶乘值,那么它必须是从 1 开始的连续整数的乘积(换句话说,它应该是除自身之外的其他因子的乘积)

long prod = 1;
long maxFactor = 1;

for(long i=2; i<=n && prod< n && n%i==0 ;i++){

    prod  = prod*i;
    if(n%prod == 0) maxFactor = prod;
}

Here n is the number whose largest factorial factor we need to find out and the final value of macFactor is the final solution.

Note: Factors of factor of a number are also the factors of the number.

If the factor is a factorial value then it must the product of the consecutive integers starting from 1 (in other words it should be the product of its factors other than itself)

似梦非梦 2024-11-25 04:08:11
#include<stdio.h>
main()
{
      int i,n;
      scanf("%d",&n);
      for(i=2;n>1&&n%i==0;i++)
      n/=i;
      printf("%d",i-1);
}

n 是输入数字

#include<stdio.h>
main()
{
      int i,n;
      scanf("%d",&n);
      for(i=2;n>1&&n%i==0;i++)
      n/=i;
      printf("%d",i-1);
}

n is the input number

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文