Open In App

Smarandache-Wellin Sequence

Improve
Improve
Like Article
Like
Save
Share
Report

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. 
 

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)(sqrt(i));
                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)(Math.sqrt(i)); 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#




using System;
// 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)(Math.Sqrt(i)); 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)(sqrt($i)); $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(Math.sqrt(i));
                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 

Time Complexity: O(n*log(n))
Auxiliary Space: O(n)
 



Last Updated : 22 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads