Home > Backend Development > C++ > body text

Smarandache-Welin sequence

王林
Release: 2023-09-09 11:45:03
forward
834 people have browsed it

Smarandache-Welin sequence

This problem consists of printing the first m terms of the Smarandache-Wellin sequence, where m is any positive integer. We will see in C an algorithm for printing the first m terms of a Smarandache-Wellin sequence. But before that, we have to understand the Smarandache-Wellin sequence.

A Smarandache-Wellin sequence is a sequence composed of Smarandache-Wellin numbers. Smarandache-Wellin numbers are integers formed by concatenating 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 in the sequence is 23, which is formed by connecting the first two consecutive prime numbers.

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

Similarly, we can conclude that the m-th item of the Smarandache-Wellin sequence is the connection of the first m consecutive prime numbers. Suppose 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 get a positive integer N, and our task is to print out 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 four consecutive prime numbers.

Input:N=7

Output: 2 23 235 2357 235711 23571113 2357111317

Explanation: The i-th item of the Smarandache-Wellin sequence is the connection of the first i consecutive prime numbers, where i is greater than or equal to 1 and less than or equal to 7.

algorithm

This method may be as simple as it looks. We know that the Nth term of the Smarandache-Wellin sequence is the connection of the first N consecutive prime numbers.

Thus, finding the first N consecutive primes will give us the first N Smarandache-Wellin numbers of the Smarandache-Wellin sequence, by further concatenating the I consecutive primes of each i-th term. We can find the first N prime numbers by following the steps below -

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

  • Use a loop to check if a number is prime until the count equals N to get the first N prime numbers. If it's a prime number, we increase the prime count by 1.

  • To determine if a number is prime, we will iterate through a for loop, starting with i=2, until the number is less than or equal to its square root. If the number is divisible by other numbers, then it is not prime because prime numbers have only two factors, the number itself and 1.

  • According to mathematics, a composite number always contains at least one factor that is less than the square root of the number. So, to determine if a number is prime, we simply iterate up to the square root of the number.

In this way, starting from 2 and checking one by one until the number of prime numbers is equal to N, we can get the first N consecutive prime numbers and store them in the array.

The next task in the problem is to print the first N items of the Smarandache-Wellin sequence. This task is very simple. We can do this by using a nested loop and iterating over an array that stores the first N consecutive prime numbers. We will iterate from 0 to the size of the array in a loop and then iterate from 0 to i in a nested loop and print all the prime numbers till i, this way we can achieve that for each i-th item The connection of the first i consecutive prime numbers.

method

We can get the desired output by following steps −

  • To check if a number is prime, create a function.

  • Create another function where you store the first N primes in an array and use that array to concatenate the first j consecutive primes to get the jth term.

  • Declare a variable named count to count the number of prime numbers. And before count equals N, each number starting from 2 is checked to see if it is prime. If it is a prime number, it is stored in the array we created.

  • For the concatenation of the first N required primes of each term, we will use a nested for loop. This is how we print the first N terms of the Smarandache-Wellin sequence.

Example

C code to solve the problem using the above 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;
}
Copy after login

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
Copy after login

Time complexity: O(N*logN), because we have to check whether each number is prime until N.

Space complexity: O(N), because we use an array of size N.

in conclusion

In this article, we learned about Smarandache-Wellin sequences and the concepts behind them. Using an efficient algorithm, we also saw how to print the first N terms of a Smarandache-Wellin sequence in C.

I hope you can clearly understand all the concepts about the problem while reading this article.

The above is the detailed content of Smarandache-Welin sequence. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template