using System; namespace studywork2{ public class work { public static void sumjiecheng() { long n = 1, t = 1, s = 0; for (; n <= 20; n++) { t *= n; s += t; } Console.WriteLine("1到20的阶乘之和为:"); Console.WriteLine(s); } public static void ifxushu(double n) { double t = 0, i = 2; for (; i < n; i++) { t = n % i; if (t == 0) { Console.WriteLine("输入的数不是质数."); break; } } if (i == n) Console.WriteLine("输入的数是质数."); } public static void quickpi() { int N = 100; int M = 500; int B = 100; int L = 4; // Machin公式 int[] s = new int[M / L]; int[] r1 = new int[N]; int[] r2 = new int[N]; int[] r3 = new int[N]; int[] r4 = new int[N]; int[] d1 = new int[N]; int[] d3 = new int[N]; int i, k, t, d2, d4, mp = M / L / 20; double Start, Finish; r1[0] = 1; r1[1] = 3; r3[0] = 4; Start = Environment.TickCount; for (k = 0; k < M / L; ++k) { t = r1[0] * B; d1[0] = t / 0x5; r1[0] = t % 0x5; t = r3[0] * B; d3[0] = t / 0xEF; r3[0] = t % 0xEF; s[k] = d1[0] - d3[0]; int tag = 0; for (i = 1; i < N; ++i) { t = r1[i] * B + d1[i - 1]; d1[i] = t / 0x19; r1[i] = t % 0x19; t = r2[i] * B + d1[i]; d2 = t / (2 * i + 1); r2[i] = t % (2 * i + 1); // t = r3[i] * B + d3[i - 1]; d3[i] = t / 0xDF21; r3[i] = t % 0xDF21; t = r4[i] * B + d3[i]; d4 = t / (2 * i + 1); r4[i] = t % (2 * i + 1); if (tag == 1) { s[k] += (d2 - d4); tag = 0; } else { s[k] += (d4 - d2); tag = 1; } } } for (i = M / L - 1; i >= 0; i--) { while (s[i] >= B) { s[i - 1]++; s[i] -= B; } while (s[i] < 0) { s[i - 1]--; s[i] += B; } } Finish = Environment.TickCount; Console.WriteLine("派的近似值为:"); Console.Write("3."); for (i = 0; i < 28; ++i) Console.Write(s[i]); Console.WriteLine(" "); Console.WriteLine("运行时间(ms):{0}", Finish - Start); } public static void Main() { double n; Console.WriteLine("......第一题......"); sumjiecheng(); Console.WriteLine("......第二题......"); Console.WriteLine("请输入要判断的数:"); n = Convert.ToInt32(Console.ReadLine()); ifxushu(n); Console.WriteLine("......第三题......"); quickpi(); String str = Console.ReadLine(); } }}

评论