首頁  >  文章  >  後端開發  >  C# 中的質數

C# 中的質數

WBOY
WBOY原創
2024-09-03 15:35:01784瀏覽

質數是大於 1 的整數,只能是數字 1 及其自身的因數,即數字「n」只能被 1 或「n」本身整除。一些眾所周知的質數是 2、3、5、7、9、11、13、17、19、23 等。 C# 程式可在質數主題中尋找給定的number 是否為質數,以及用於顯示給定範圍內的所有質數。這可以在 C# 程式中透過使用各種迴圈和條件語句定義邏輯來實現,例如 for 迴圈、if 條件、if else 條件、while 迴圈等。

C# 中的質數範例

讓我們嘗試透過以下程式範例來概念化素數。

範例#1

C# 程式列印 1 到 100 之間的所有質數列表。

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args) // this function defines the entry point
{
bool Prime = true;
Console.WriteLine("Prime Numbers between 1 to 100 : ");
for (int a = 2; a <= 100; a++) //upper limit and lower limit are defined
{
for (int b = 2; b <= 100; b++)// base logic for the primality
{
if (a != b && a % b == 0) //modulo operators employed
{
Prime = false;
break;
}
}
if (Prime)
{
Console.Write("\t" + a); //printing the correct variable
}
Prime = true;
}
Console.ReadKey(); //hold the output screen
}
}
}

輸出:

C# 中的質數

程式說明: 上面的程式是使用循環和條件運算子來決定固定數字範圍內的素數的經典範例。上面的程式使用自然數的下限,即2 定義「a」為2 到99 範圍內的自然數,運算後增量為1,下一步使用具有類似範圍的變數「b」但受限於一個條件,其上限總是小於'a'。然後循環遍歷範圍並使用變數 a 除以除數 b 的自然模運算。

如果 a 可以被 b 整除,則模運算子傳回 0,表示 b 作為較小的自然數是合數 a 的因數。我們使用布林參數 Prime 作為標誌,以防我們收到不等於 0 的 a % b 值。現在,我們使用條件運算子在輸出控制台中列印數字,以防我們收到的數字是質數。

範例#2

使用 for 迴圈檢查素數

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
int n, a, m = 0, PrimeIndicator = 0;
Console.Write("Please enter the number to check for Primality: ");
n = int.Parse(Console.ReadLine());
m = n / 2;
for (a = 2; a <= m; a++)
{
if (n % a == 0)
{
Console.Write("The entered number is not  a Prime number .");
PrimeIndicator = 1;
break;
}
}
if (PrimeIndicator == 0)
Console.Write("The entered number is a Prime number . ");
}
}
}

輸出:

C# 中的質數

C# 中的質數

程式說明: 上述程式使用for迴圈定義素數條件。輸入讀取欄位以擷取使用者輸入並指派給變數 n,計數器從值 2 解析到值 n-1 並測試整除性條件以確定該數字是否為質數。涉及的附加功能該程式使用值為 n/2 或剛好是初始使用者輸入一半的變數 m,該程式僅解析最多為 m 值的循環。

範例 #3

使用 while 迴圈的質數。

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n, i, a;
Console.Write("Enter any number: ");
n = Convert.ToInt32(Console.ReadLine()); // read the user input
a = 0;
i = 2;
while (i <= n / 2)
{
if (n % i == 0)
{
a = 1;
break;
}
i++;
}
if (a == 0)
Console.WriteLine(n + " Prime Number as there are no factors");
else
Console.WriteLine(n + " not a Prime Number ");
Console.ReadLine();
}
}
}

輸出:

C# 中的質數

C# 中的質數

程式說明: 上面的程式示範了在 C# 中使用 while 迴圈來決定數字素數的過程。上面的程式使用控制讀取命令讀取使用者輸入,並在範圍 2 上解析使用者輸入除以 2 的值,以確定用於測試數字素數的標誌的值。循環內部賦值,並根據a的值顯示結果

結論

質數是大於 1 的自然數,只有 1 和它本身的因數。合成數可以分解為質數的因數,這些數稱為素因數。上面所示的各種程式為我們提供了使用 do、for、while 迴圈等迴圈來實現任意數字的質數的方法。儘管上述所有程序的實現背後的邏輯是透過使用模運算子進行操作來查找數字的因子,但其實現是根據循環的選擇在不同的點進行控制的。

以上是C# 中的質數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn