正文

c#作业2007-04-18 21:32:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/7zeal/24996.html

分享到:

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();
        }
    }
}

 

阅读(1995) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册