幾個數學知識備忘

來源:互聯網
上載者:User
一:階乘相關
  求N的階乘的方法 :
       public static Fab(int n)
      {
        if(n==1) return 1;
       return n * Fab(n-1);
   }
      問:1000的階乘未尾有幾個0?
  解: 要產生0,那麼分解後的最小因子一定要有2和5,而2個數一定會多於5,那麼只需要求出5的個數就可以了。
  那麼結果 :1000/5=200,200/5=40,40/5=8,(int)8/5=1
   共有200+40+8+1=249個0。

輸出所有水仙花數
一個N(N>=3)位元,其值 等於每位N次方之和

 1            for (int i = 100; i <= 999; i++)
 2            {
 3
 4                if (Math.Pow(i / 100, 3) +
 5                        Math.Pow((i / 10) - (i / 100) * 10, 3) +
 6                                Math.Pow(i - i / 10 * 10, 3) == i)
 7                {
 8                    System.Console.WriteLine(
 9                        "{0}:[{1}|{2}|{3}]", i, i / 100, (i / 10) - (i / 100) * 10, i - i / 10 * 10);
10                }
11            }

輸出10000以內的完全數
又稱完美數,它是指真因子之和等於自身的自然數

 1            for (int i = 1; i <= 10000; i++)
 2            {
 3                int k = 0;
 4                for (int j = 1; j <= i - 1; j++)
 5                {
 6                    if ((i % j) == 0)
 7                    {
 8                        k += j;
 9                    }
10                }
11                if (k == i)
12                {
13                    System.Console.WriteLine(i);
14                }
15            }

輸出10000以內的相親數
兩個正整數,X的真因子之和等於Y的真因子之和

 

 1            for (int i = 1; i <= 10000; i++)
 2            {
 3                int x = i;//X為初值
 4                int y = 0;
 5                for (int j = 1; j <= x - 1; j++)//計算X的真因子
 6                {
 7                    if ((x % j) == 0)
 8                    { y += j; }
 9                }//Y=X的真因子之合
10                if (y == x)//完美數
11                { continue; }
12                x = 0;
13                for (int j = 1; j <= y - 1; j++)//計算Y的真因子
14                {
15                    if ((y % j) == 0)
16                    { x += j; }
17                }//X=Y的真因子之合
18                if (x == i)
19                {
20                    System.Console.WriteLine("{0}--{1}", x, y);
21                }
22            }

部分摘錄自:http://www.cnblogs.com/shyleoking/articles/635591.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.