C#

来源:互联网 发布:手机总是弹出登录网络 编辑:程序博客网 时间:2024/06/07 02:11
[csharp] view plain copy
 print?
  1. // --------------------------------------------------------------------------------------------------------------------  
  2. // <copyright file="Program.cs" company="Chimomo's Company">  
  3. // Respect the work.  
  4. // </copyright>  
  5. // <summary>  
  6. // The program.  
  7. // </summary>  
  8. // --------------------------------------------------------------------------------------------------------------------  
  9.   
  10. namespace CSharpLearning  
  11. {  
  12.     using System;  
  13.   
  14.     /// <summary>  
  15.     /// The program.  
  16.     /// </summary>  
  17.     public static class Program  
  18.     {  
  19.         /// <summary>  
  20.         /// The main.  
  21.         /// </summary>  
  22.         /// <param name="args">  
  23.         /// The args.  
  24.         /// </param>  
  25.         public static void Main(string[] args)  
  26.         {  
  27.             string fac100 = Factorial(100).ToString("F0");  
  28.             Console.WriteLine("The factorial of 100 is : {0}", fac100);  
  29.             for (int i = 0; i <= fac100.Length - 4; i++)  
  30.             {  
  31.                 string substr = fac100.Substring(i, 4);  
  32.                 if (CheckPrime(Convert.ToInt32(substr)))  
  33.                 {  
  34.                     Console.WriteLine("The expected result found and it is : " + substr);  
  35.                     return;  
  36.                 }  
  37.             }  
  38.   
  39.             Console.WriteLine("No result as expected!!");  
  40.         }  
  41.   
  42.         /// <summary>  
  43.         /// The factorial.  
  44.         /// </summary>  
  45.         /// <param name="n">  
  46.         /// The n.  
  47.         /// </param>  
  48.         /// <returns>  
  49.         /// The <see cref="double"/>.  
  50.         /// </returns>  
  51.         public static double Factorial(int n)  
  52.         {  
  53.             double result = 1;  
  54.             for (int i = 1; i <= n; i++)  
  55.             {  
  56.                 result *= i;  
  57.             }  
  58.   
  59.             return result;  
  60.         }  
  61.   
  62.         /// <summary>  
  63.         /// The check prime.  
  64.         /// </summary>  
  65.         /// <param name="n">  
  66.         /// The n.  
  67.         /// </param>  
  68.         /// <returns>  
  69.         /// The <see cref="bool"/>.  
  70.         /// </returns>  
  71.         public static bool CheckPrime(int n)  
  72.         {  
  73.             if (n == 1 || n == 2)  
  74.             {  
  75.                 return true;  
  76.             }  
  77.   
  78.             int squareRoot = Convert.ToInt32(Math.Sqrt(n));  
  79.             for (int i = squareRoot; i > 1; i--)  
  80.             {  
  81.                 if (n % i == 0)  
  82.                 {  
  83.                     return false;  
  84.                 }  
  85.             }  
  86.   
  87.             return true;  
  88.         }  
  89.     }  
  90. }  
  91.   
  92. // Output:  
  93. /* 
  94. The factorial of 100 is : 93326215443944100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 
  95. The expected result found and it is : 2621 

  1. */  
  2. 转自:http://blog.csdn.net/troubleshooter/article/details/5506027

原创粉丝点击