Smarandache-Wellin Sequence


The problem includes printing first m terms of Smarandache-Wellin Sequence where m is any positive integer. We will see the algorithm to print the first m term of Smarandache-Wellin Sequence in C++. But before that we must know about the Smarandache-Wellin sequence.

A Smarandache-Wellin sequence is a sequence of Smarandache-Wellin numbers. Smarandache-Wellin numbers are the integers which are formed by concatenation of the consecutive prime numbers. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23….

  • The first Smarandache-Wellin number of the sequence is 2.

  • The second number of the sequence is 23, which is formed by the concatenation of the first two consecutive prime numbers.

  • The third number of the sequence is 235, which can be said to be formed by the concatenation of the first three consecutive prime numbers.

Similarly, we can conclude that the mth term of the Smarandache-Wellin sequence is nothing but the concatenation of the first m consecutive prime numbers. Let’s say we want the 6th Smarandache-Wellin number then it will be the concatenation of the first 6 consecutive numbers which is 23571113.

In the above problem, we will be given a positive integer N, our task is to print the first N Smarandache-Wellin numbers of the Smarandache-Wellin sequence. For example,

INPUT: N=4

OUTPUT: 2 23 235 2357

Explanation: These are the first four numbers of the Smarandache-Wellin sequence formed by the first 4 consecutive prime numbers respectively.

INPUT: N=7

OUTPUT: 2 23 235 2357 235711 23571113 2357111317

Explanation: All the ith term of the Smarandache-Wellin sequence is the concatenation of the first i consecutive prime numbers where i is greater than or equal to 1 and less than or equal to 7.

Algorithm

The approach could be just as simple as it looks. We know that the Nth term of the Smarandache-Wellin sequence is the concatenation of the first N consecutive prime numbers.

Therefore, finding out the first N consecutive prime numbers will give us the first N Smarandache-Wellin numbers of the Smarandache-Wellin sequence by further concatenating I consecutive prime numbers for every ith term. We can find out the first N prime numbers following the below steps −

  • For storing the count of prime numbers to get the first N consecutive prime numbers, we will create a variable.

  • Checking if the number is a prime number or not using a loop until count equals N to get first N prime numbers. If it is a prime number we will increase the count of prime numbers by 1.

  • In order to determine whether a number is a prime number or not, we will iterate through a for loop starting at i=2 until the number is less than or equal to its square root. If the number is divisible by any other number, it is not a prime number because a prime number only has two factors, the number itself and 1.

  • A composite number always contains at least one factor that is less than the square root of the number, according to mathematics. Because of this, to determine whether or not a number is a prime, we simply iterate until the number's square root.

In this way, iterating until the count of prime numbers equals N and checking for every number starting from 2, we can get the first N consecutive prime numbers and store them in the array.

The next task of the problem, which is printing the first N term of the Smarandache- Wellin sequence, is quite simple. We can achieve it using nested loops and iterating over the array where we stored first N consecutive prime numbers. We will iterate in a loop from 0 to size of the array and then in a nested loop from 0 to i and print all the prime numbers until i through which we can achieve concatenation of first i consecutive prime numbers for every ith term.

Approach

We can get our required output using these steps −

  • To check if the number is a prime number or not, make a function.

  • Make another function where we will store first N prime numbers in an array and use the array to concatenate first j consecutive prime numbers to get the jth term.

  • Declare a variable named count to account for the count of prime numbers. And until count equals N checking every number starting from 2 if it is a prime number or not. If it is a prime number, store it in the array we created.

  • For concatenating the first required prime numbers for every term, we will use nested for loops. This is how we can print the first N terms of the Smarandache-Wellin sequence.

Example

The C++ code for solving the problem using aforementioned algorithm −

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

//function to check if the number is a prime number or not
bool check(int N){
   for(int i= 2; i <=sqrt(N); i++){ //iterating to check if the number has any divisor other than 1 and number itself
      if(N % i == 0){ //if it has return false since it is not a prime number
         return false;
      }
   }
   return true; //return true if it satisfies all the conditions
}

//function to print first N terms of Smarandache-Wellin sequence
//using an array to store first N consecutive prime numbers
void ans(int N){
   int ans[N];
   int count=0; //to count number of prime numbers
   for(int i=2;count<N;i++){ //for storing first N consecutive prime numbers in the array
      if(check(i)){
         ans[count]=i; //if the number is prime store it in the array
         count++; //increase count
      } else {
         continue;
      }
   }
   cout<<"The first "<<N<<" terms of Smarandache-Wellin sequence are: ";
   for(int i=0;i<N;i++){ //for printing first N terms of Smarandache-Wellin sequence
      for(int a=0;a<=i;a++){ //for concatenating first a prime numbers for ath term
         cout<<ans[a];
      }
      cout<<" ";
   }
   cout<<endl;
}
int main(){
   int N=6;
   ans(N);
   N=12;
   ans(N);
   return 0;
}

Output

The first 6 terms of Smarandache-Wellin sequence are: 2 23 235 2357 235711 23571113
The first 12 terms of Smarandache-Wellin sequence are: 2 23 235 2357 235711 23571113 2357111317 235711131719 23571113171923 2357111317192329 235711131719232931 23571113171923293137

Time Complexity : O(N*logN), since we are checking for every number until N if it is a prime or not.

Space Complexity : O(N), since we used an array of size N.

Conclusion

We have learned about the Smarandache-Wellin sequence and the concept behind it in this article. Using an efficient algorithm, we also see how to print first N terms of the Smarandache-Wellin sequence in C++.

I hope you find this article helpful in clearing all your concepts regarding the problem.

Updated on: 16-Mar-2023

210 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements