Smarandache-Wellin 序列
這個問題包括列印 Smarandache-Wellin 序列的前 m 項,其中 m 是任何正整數。我們將看到用 C++ 列印 Smarandache-Wellin 序列前 m 項的演算法。但在那之前,我們必須瞭解 Smarandache-Wellin 序列。
一個Smarandache-Wellin 序列是由 Smarandache-Wellin 數構成的序列。Smarandache-Wellin 數是由連續素數連線而成的整數。前幾個素數是 2, 3, 5, 7, 11, 13, 17, 19, 23……
該序列的第一個 Smarandache-Wellin 數是 2。
該序列的第二個數字是 23,它是前兩個連續素數連線而成的。
該序列的第三個數字是 235,可以說它是前三個連續素數連線而成的。
同樣,我們可以得出結論,Smarandache-Wellin 序列的第 m 項就是前 m 個連續素數的連線。假設我們想要第 6 個 Smarandache-Wellin 數,那麼它將是前 6 個連續數字的連線,即 23571113。
在上述問題中,我們將得到一個正整數 N,我們的任務是列印 Smarandache-Wellin 序列的前 N 個 Smarandache-Wellin 數。例如:
輸入:N=4
輸出: 2 23 235 2357
解釋:這些是由前 4 個連續素數分別構成的 Smarandache-Wellin 序列的前四個數字。
輸入:N=7
輸出: 2 23 235 2357 235711 23571113 2357111317
解釋:Smarandache-Wellin 序列的每一項都是前 i 個連續素數的連線,其中 i 大於等於 1 且小於等於 7。
演算法
這種方法可能和看起來一樣簡單。我們知道 Smarandache-Wellin 序列的第 N 項是前 N 個連續素數的連線。
因此,找出前 N 個連續素數,透過進一步連線每項的 i 個連續素數,就可以得到 Smarandache-Wellin 序列的前 N 個 Smarandache-Wellin 數。我們可以按照以下步驟找出前 N 個素數:
為了儲存素數的數量以獲得前 N 個連續素數,我們將建立一個變數。
使用迴圈檢查數字是否是素數,直到計數等於 N 以獲得前 N 個素數。如果它是素數,我們將把素數計數增加 1。
為了確定一個數字是否是素數,我們將迭代一個 for 迴圈,從 i=2 開始,直到該數字小於或等於其平方根。如果該數字能被任何其他數字整除,則它不是素數,因為素數只有兩個因子,即該數字本身和 1。
根據數學原理,合數總是至少包含一個小於該數字平方根的因子。正因為如此,為了確定一個數字是否是素數,我們只需迭代到該數字的平方根。
透過這種方式,迭代直到素數計數等於 N,並檢查從 2 開始的每個數字,我們可以得到前 N 個連續素數並將它們儲存在陣列中。
問題的下一個任務,即列印 Smarandache-Wellin 序列的前 N 項,非常簡單。我們可以使用巢狀迴圈並在我們儲存前 N 個連續素數的陣列上進行迭代來實現這一點。我們將迭代一個從 0 到陣列大小的迴圈,然後在另一個巢狀迴圈中從 0 到 i,列印所有直到 i 的素數,透過這種方式,我們可以實現每項前 i 個連續素數的連線。
方法
我們可以使用以下步驟獲得所需輸出:
編寫一個函式來檢查數字是否是素數。
編寫另一個函式,我們將前 N 個素數儲存在一個數組中,並使用該陣列連線前 j 個連續素數以獲得第 j 項。
宣告一個名為 count 的變數來計算素數的個數。直到 count 等於 N,檢查從 2 開始的每個數字是否是素數。如果它是素數,則將其儲存在我們建立的陣列中。
為了連線每一項所需的前幾個素數,我們將使用巢狀 for 迴圈。這就是我們如何列印 Smarandache-Wellin 序列的前 N 項。
示例
使用上述演算法解決問題的 C++ 程式碼:
#include <iostream> #include <bits/stdc++.h> using namespace std; //function to check if the number is a prime number or not bool check(int N){ for(int i= 2; i <=sqrt(N); i++){ //iterating to check if the number has any divisor other than 1 and number itself if(N % i == 0){ //if it has return false since it is not a prime number return false; } } return true; //return true if it satisfies all the conditions } //function to print first N terms of Smarandache-Wellin sequence //using an array to store first N consecutive prime numbers void ans(int N){ int ans[N]; int count=0; //to count number of prime numbers for(int i=2;count<N;i++){ //for storing first N consecutive prime numbers in the array if(check(i)){ ans[count]=i; //if the number is prime store it in the array count++; //increase count } else { continue; } } cout<<"The first "<<N<<" terms of Smarandache-Wellin sequence are: "; for(int i=0;i<N;i++){ //for printing first N terms of Smarandache-Wellin sequence for(int a=0;a<=i;a++){ //for concatenating first a prime numbers for ath term cout<<ans[a]; } cout<<" "; } cout<<endl; } int main(){ int N=6; ans(N); N=12; ans(N); return 0; }
輸出
The first 6 terms of Smarandache-Wellin sequence are: 2 23 235 2357 235711 23571113 The first 12 terms of Smarandache-Wellin sequence are: 2 23 235 2357 235711 23571113 2357111317 235711131719 23571113171923 2357111317192329 235711131719232931 23571113171923293137
時間複雜度:O(N*logN),因為我們正在檢查從 2 到 N 的每個數字是否是素數。
空間複雜度:O(N),因為我們使用了大小為 N 的陣列。
結論
在本文中,我們學習了 Smarandache-Wellin 序列及其背後的概念。我們還使用有效的演算法瞭解瞭如何在 C++ 中列印 Smarandache-Wellin 序列的前 N 項。
我希望本文能幫助您理解所有關於這個問題的概念。