📜  Smarandache-Wellin序列

📅  最后修改于: 2021-04-28 16:51:11             🧑  作者: Mango

给定数字“ n”,生成Smarandache-Wellin序列的前“ n”项。
Smarandache-Wellin序列是由Smarandache-Wellin数形成的序列。组成序列的每个Smarandache-Wellin数都是通过串联从第一个质数(即2)开始的连续质数获得的。因此,序列的第一项是2,第二项是23,第三项是235,… 。同样,第n个项是通过将第一个n个质数从第一个质数(即2)开始串联而成的。

例子:

Input : 5
Output : 2 23 235 2357 235711

Input : 10
Output : 2 23 235 2357 235711 23571113 2357111317 235711131719 23571113171923
2357111317192329

方法:
1)最初找到第一个’n’质数并将它们存储在列表中。
2)接下来,连接列表中的每个术语,从第一个术语开始,然后每次将连接术语的长度增加一个。
3)每次继续打印如此形成的连接项以生成序列。

以下是Python的实现。

C++
// C++ program to print the first 
// 'n' terms of the Smarandache-Wellin
// Sequence 
#include
using namespace std;
  
// Function to collect 
// first 'n' prime numbers 
void primes(int n) 
{ 
    int i = 2; 
    int j = 0; 
      
    // List to store 
    // first 'n' primes 
    int result[n]; 
    int z = 0; 
    while(j < n) 
    { 
        bool flag = true; 
        for(int item = 2;
                item <= (int)(i * 1 / 2); 
                item++) 
           if(i % item == 0 && i != item) 
           { 
               flag = false; 
               break; 
            } 
              
        if (flag)
        { 
            result[z++] = i; 
            j += 1; 
        } 
        i += 1; 
    } 
  
    for(i = 0; i < 5; i++) 
    { 
       for(j = 0; j <= i; j++) 
          cout << result[j];
       cout << " "; 
    } 
} 
  
// Function to generate 
// Smarandache-Wellin Sequence 
void smar_wln(int n) 
{ 
      
    // Storing the first 'n' 
    // prime numbers in a list 
    primes(n); 
      
} 
  
// Driver Code 
int main() 
{ 
    int n = 5; 
      
    cout << "First " << n 
         << " terms of the Sequence are" 
         << endl; 
  
    smar_wln(n); 
} 
  
// This code is contributed by Ritik Bansal


Java
// Java program to print the 
// first 'n' terms of the
// Smarandache-Wellin Sequence
  
class GFG{
// Function to collect
// first 'n' prime numbers
static void primes(int n)
{
    int i = 2;
    int j = 0;
      
    // List to store
    // first 'n' primes
    int[] result=new int[n];
    int z = 0;
    while(j < n)
    {
        boolean flag = true;
        for(int item = 2;item <= (int)(i * 1 / 2); item++)
            if(i % item == 0 && i != item)
            {
                flag = false;
                break;
            }
        if (flag)
        {
            result[z++] = i;
            j += 1;
        }
        i += 1;
    }
  
    for(i = 0; i < result.length; i++)
    {
        for(j = 0; j <= i; j++)
            System.out.print(result[j]);
        System.out.print(" ");
    }
}
  
// Function to generate 
// Smarandache-Wellin Sequence
static void smar_wln(int n)
{
    // Storing the first 'n'
    // prime numbers in a list
    primes(n);
      
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
System.out.println("First "+n+" terms of the Sequence are");
smar_wln(n);
}
}
// This code is contributed
// by mits


Python3
# Python program to print the first 'n' terms
# of the Smarandache-Wellin Sequence
  
from __future__ import print_function
  
# Function to collect first 'n' prime numbers
  
def primes(n):
    i, j = 2, 0
    # List to store first 'n' primes
    result = []
    while j < n:
        flag = True
        for item in range(2, int(i**0.5)+1):
            if i % item == 0 and i != item:
                flag = False
                break
        if flag:
            result.append(i)
            j += 1
        i += 1
    return result
  
# Function to generate Smarandache-Wellin
# Sequence
  
def smar_wln(n):
    # Storing the first 'n' prime numbers in
    # a list
    arr = primes(n)
    for i in range(0, len(arr)):
        for j in range(0, i + 1):
            print(arr[j], end ='')
        print(end =' ')
  
# Driver Method
  
if __name__=='__main__':
    n = 5
    print('First {} terms of the Sequence are\n'.format(n))
    smar_wln(n)


C#
// C# program to print the 
// first 'n' terms of the
// Smarandache-Wellin Sequence
class GFG
{
// Function to collect
// first 'n' prime numbers
static void primes(int n)
{
    int i = 2;
    int j = 0;
      
    // List to store
    // first 'n' primes
    int[] result = new int[n];
    int z = 0;
    while(j < n)
    {
        bool flag = true;
        for(int item = 2; 
                item <= (int)(i * 1 / 2); item++)
            if(i % item == 0 && i != item)
            {
                flag = false;
                break;
            }
        if (flag)
        {
            result[z++] = i;
            j += 1;
        }
        i += 1;
    }
  
    for(i = 0; i < result.Length; i++)
    {
        for(j = 0; j <= i; j++)
            System.Console.Write(result[j]);
        System.Console.Write(" ");
    }
}
  
// Function to generate 
// Smarandache-Wellin Sequence
static void smar_wln(int n)
{
    // Storing the first 'n'
    // prime numbers in a list
    primes(n);
      
}
  
// Driver Code
static void Main()
{
    int n = 5;
    System.Console.WriteLine("First " + n + 
             " terms of the Sequence are");
    smar_wln(n);
}
}
  
// This code is contributed by mits


PHP


输出

First 5 terms of the Sequence are

2 23 235 2357 235711