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.
C++
#include<bits/stdc++.h>
using namespace std;
void primes( int n)
{
int i = 2;
int j = 0;
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 << " " ;
}
}
void smar_wln( int n)
{
primes(n);
}
int main()
{
int n = 5;
cout << "First " << n
<< " terms of the Sequence are"
<< endl;
smar_wln(n);
}
|
Java
class GFG{
static void primes( int n)
{
int i = 2 ;
int j = 0 ;
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( " " );
}
}
static void smar_wln( int n)
{
primes(n);
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println( "First " +n+ " terms of the Sequence are" );
smar_wln(n);
}
}
|
Python3
from __future__ import print_function
def primes(n):
i, j = 2 , 0
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
def smar_wln(n):
arr = primes(n)
for i in range ( 0 , len (arr)):
for j in range ( 0 , i + 1 ):
print (arr[j], end = '')
print (end = ' ' )
if __name__ = = '__main__' :
n = 5
print ( 'First {} terms of the Sequence are\n' . format (n))
smar_wln(n)
|
C#
using System;
class GFG
{
static void primes( int n)
{
int i = 2;
int j = 0;
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( " " );
}
}
static void smar_wln( int n)
{
primes(n);
}
static void Main()
{
int n = 5;
System.Console.WriteLine( "First " + n +
" terms of the Sequence are" );
smar_wln(n);
}
}
|
PHP
<?php
function primes( $n )
{
$i = 2;
$j = 0;
$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 smar_wln( $n )
{
$arr = primes( $n );
for ( $i = 0;
$i < count ( $arr ); $i ++)
{
for ( $j = 0; $j <= $i ; $j ++)
echo $arr [ $j ];
echo " " ;
}
}
$n = 5;
echo "First $n terms of the" .
" Sequence are\n" ;
smar_wln( $n );
?>
|
Javascript
<script>
function primes(n)
{
var i = 2;
var j = 0;
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 smar_wln(n)
{
primes(n);
}
var n = 5;
document.write( "First " + n
+ " terms of the Sequence are<br>" );
smar_wln(n);
</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
Share your thoughts in the comments
Please Login to comment...