列出1~1000質數程式

Home Home
引用 | 編輯 tutelar
2007-05-22 04:27
樓主
推文 x0
這是小弟自己寫出來列出質數的程式
Po上來給大家參考參考
若還有可以修改或更精簡的
麻煩指教一番~
感激^^

static void Prime() //第七題
{
int D = 0;
Console. ..

訪客只能看到部份內容,免費 加入會員



獻花 x1
引用 | 編輯 ety
2007-06-02 07:03
1樓
  
迴圈可以再少幾次唷!

想想看~ 除了2, 還有哪個質數是雙數? 應該沒有才對...^^

1跟2可以直接輸出不用判斷, 然後3開始, 以 i += 2 來執行!

ex: for (int i = 3; i < 1000; i += 2)

獻花 x1
引用 | 編輯 a5266290x
2008-05-23 03:09
2樓
  
這樣應該更快八!
    static void Main(string[] args)
    {
        bool N = true;
        string Str = "2 ";
        Int32 number = 2;

        Console.Write("{0,5}","1");
        for (int i = 3; i <= 3000; i++)
        {
          string[] StrEnd = Str.Split();

          for (int k = 0; k < StrEnd.GetUpperBound(0); k++)
          {
            N = true;
            if (i % Convert.ToInt32(StrEnd[k]) == 0)
            {
                N = false;
                break;
            }

          }
          if (N == true)
          {
            Str = Str + Convert.ToString(i) + " ";
            Console.Write("{0,5}", Convert.ToString(i));
            number += 1;
          }
        }
        Console.WriteLine();
        Console.WriteLine ("質因數個數"+Convert .ToString (number ));


    }

獻花 x0
引用 | 編輯 jrhau520
2009-09-17 14:39
3樓
  
我是寫這樣子 =ˇ= 分享一下 新手上路學習中 表情

class Program
  {
    static void Main(string[] args)
    {
        for (int i = 2; i <= 1000; i++)
        {
          for (int j = 2; j <= i; j++)
          {
            if (i == j)
                Console.WriteLine("{0}", j);
            else if (i % j == 0)
                break;
            else
                continue;
          }
        }
        Console.ReadLine();
    }
  }

獻花 x0
引用 | 編輯 Kizuna
2009-09-17 22:28
4樓
  
複製程式
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("2 is prime\n");
            for (int p = 3; p <= 1000; p+=2)
            {
                bool Is = true;
                for (int i = 2; i <= Math.Sqrt((double)p); i++)
                    if (p % i == 0)
                    {
                        Is = false;
                        break;
                    }
                if (Is)
                    Console.Write(p + " is prime\n");
            }

            Console.ReadLine();
        }
    }
}


獻花 x0
引用 | 編輯 jerry520
2012-04-07 18:00
5樓
  
目前數學家雖然沒有求值數的公式 , 但是卻有檢驗是否為值數的技術

就是利用費馬(Pierre de Fermat)定理來認定

後來由工程師Miller-Rabin改良技術 , 讓確認質數的效率提高

去看看我的找質數網站

http://ho520.myweb.hinet.net/

可以讓你求出任何100位數內的大質數

獻花 x0
引用 | 編輯 iamboss123
2014-01-12 14:34
6樓
  
其實可以窮舉 呵呵

獻花 x0