# 100的阶层真的算不出来吗？

2015/09/08 21:45

#include<stdio.h>

int main()
{
int a,count =0;
for(a=5;a<=100;a+=5)
{
++count;
if(!(a%25))
++count;
}
printf("100!的尾数有%d个零。\n",count);
return 0;
}

#include<stdio.h>

int main()
{
int n;
printf("请输入N：\n");
scanf("%d",&n);
if(n<0)
printf("%d的阶层无意义。\n",n);
else if(n<=4)
printf("%d的阶层的尾数没有零。\n");
else
{
int a,count =0;
for(a=5;a<=n;a+=5)
{
++count;
if(!(a%25))
++count;
}
printf("100!的尾数有%d个零。\n",count);
}
return 0;
}

(define (fact n) (if (= n 1) 1 (* n (fact (- n 1)))))
;Value: fact


1000的阶层也能求，截图为证……

………………

1x2=2 2x3=6 6x4=24 24x5=120 120x6=720 720x7=5040 5040x8=40320 40320x9=362880 362880x10=3628800 3628800x11=39916800 3991680x12=479001600 47900160x13=6227020800 622702080x14=87178291200 8717829120x15=1307674368000 …… ……

#include<stdio.h>

int main()
{
int a,count =0;
for(a=5;a<=200;a+=5)
{
++count;
if(!(a%25))
{
++count;
if(!(a%125))
++count;
}

}
printf("200!的尾数有%d个零。\n",count);
return 0;
}


// C# Code
public static class Program
{
public static void Main(string[] args)
{
string fac100 = Factorial(100).ToString("F0");
Console.WriteLine("The factorial of 100 is : {0}", fac100);

for (int i = 0; i <= fac100.Length - 4; i++)
{
string substr = fac100.Substring(i, 4);
if (CheckPrime(Convert.ToInt32(substr)))
{
Console.WriteLine("The expected result found and it is : " + substr);
return;
}
}
Console.WriteLine("No result as expected!!");
}

public static double Factorial(int n)
{
double result = 1;
for (int i = 1; i <= n; i++)
result *= i;
return result;
}

public static bool CheckPrime(int n)
{
if (n == 1 || n == 2)
return true;
int squareRoot = Convert.ToInt32(Math.Sqrt(n));
for (int i = squareRoot; i > 1; i--)
if (n % i == 0)
return false;
return true;
}
}
// C++ Code
#include <iostream>
#include <math.h>
using namespace std;

double Factorial(int n)
{
double result = 1;
for (int i = 1; i <= n; i++)
result *= i;
return result;
}

bool CheckPrime(long n)
{
if (n == 1 || n == 2)
return true;
long squareRoot = (long)sqrt(n);
for (long i = squareRoot; i > 1; i--)
if (n % i == 0)
return false;
return true;
}

int main(int argc, char *argv[])
{
char buf[1024] = { '\0' };
sprintf_s(buf, "%.f", Factorial(100));
cout << "The factorial of 100 is : " << buf << endl;

char substr[5] = { '\0' };
for (int i = 0; i <= strlen(buf) - 4; i++)
{
memcpy(substr, buf + i, 4);
if (CheckPrime(atol(substr)))
{
cout << "The expected result found and it is : " << substr << endl;
return 0;
}
}
cout << "No result as expected!!";
return 0;
}

// C Code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>

double Factorial(int n)
{
double result = 1;
int i;
for (i = 1; i <= n; i++)
result *= i;
return result;
}

bool CheckPrime(long n)
{
if (n == 1 || n == 2)
return true;
long squareRoot = (long)sqrt(n);
long i;
for (i = squareRoot; i > 1; i--)
if (n % i == 0)
return false;
return true;
}

int main(int argc, char *argv[])
{
char buf[1024] = { '\0' };
sprintf(buf, "%.f", Factorial(100));
printf("The factorial of 100 is : %s\n",buf);

char substr[5] = { '\0' };
int i;
for (i = 0; i <= strlen(buf) - 4; i++)
{
memcpy(substr, buf + i, 4);
if (CheckPrime(atol(substr)))
{
printf("The expected result found and it is : %s\n",substr);
return 0;
}
}
printf("No result as expected!!\n");
return 0;
}

http://blog.csdn.net/nomasp

0
0 收藏

0 评论
0 收藏
0