Skip to content
Related Articles

Related Articles

Improve Article

Smarandache-Wellin Sequence

  • Difficulty Level : Medium
  • Last Updated : 07 May, 2021

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. 
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

C++




// C++ program to print the first
// 'n' terms of the Smarandache-Wellin
// Sequence
#include<bits/stdc++.h>
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




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

Javascript




<script>
 
// Javascript program to print the first
// 'n' terms of the Smarandache-Wellin
// Sequence
 
// Function to collect
// first 'n' prime numbers
function primes(n)
{
    var i = 2;
    var j = 0;
     
    // List to store
    // first 'n' primes
    var result = Array(n)
    var z = 0;
    while(j < n)
    {
        var flag = true;
        for(var item = 2;
                item <= parseInt(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++)
          document.write( result[j]);
       document.write(" ");
    }
}
 
// Function to generate
// Smarandache-Wellin Sequence
function smar_wln(n)
{
     
    // Storing the first 'n'
    // prime numbers in a list
    primes(n);
     
}
 
// Driver Code
var n = 5;
 
document.write( "First " + n
     + " terms of the Sequence are<br>" );
smar_wln(n);
 
// This code is contributed by rrrtnx.
</script>

Output 
 

First 5 terms of the Sequence are

2 23 235 2357 235711 

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :