Smarandache-Wellin Sequence

Given a number ‘n’, generate the first ‘n’ terms of the Smarandache-Wellin Sequence.
The Smarandache-Wellin Sequence is a sequence formed by the Smarandache-Wellin numbers. Each Smarandache-Wellin number that make up the sequence is obtained by concatenating the consecutive prime numbers beginning from the first prime number i.e, 2. Thus, the first term of the sequence is 2, second term is 23, third term is 235, …. Similarly, the ‘n’th term is made up by concatenating the first ‘n’ prime numbers beginning from the first prime number i.e, 2.

Examples:

Input : 5
Output : 2 23 235 2357 235711

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

Approach:
1) Initially find the first ‘n’ prime numbers and store them in a list.
2) Next, concatenate each term of the list beginning from the first term and increasing the length of the concatenated term each time by one.
3) Keep printing the concatenated terms so formed, each time, to generate the sequence.



Below is the implementation in Python.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print the 
// first 'n' terms of the
// Smarandache-Wellin Sequence
  
// Function to collect
// first 'n' prime numbers
function primes($n)
{
    $i = 2;
    $j = 0;
      
    // List to store
    // first 'n' primes
    $result;
    $z = 0;
    while($j < $n)
    {
        $flag = true;
        for($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;
    }
    return $result;
}
  
// Function to generate 
// Smarandache-Wellin Sequence
function smar_wln($n)
{
    // Storing the first 'n'
    // prime numbers in a list
    $arr = primes($n);
    for($i = 0; 
        $i < count($arr); $i++)
    {
        for($j = 0; $j <= $i; $j++)
            echo $arr[$j];
        echo " ";
    }
}
// Driver Code
$n = 5;
echo "First $n terms of the"
        " Sequence are\n";
smar_wln($n);
  
// This code is contributed
// by mits 
?>

chevron_right



Output

First 5 terms of the Sequence are

2 23 235 2357 235711 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar