Find primes in range [1, N] which also belongs to a Tribonacci series
Last Updated :
28 Apr, 2022
Given a number N, the task is to find the primes within range [1, N] which is also a part of a Tribonacci series starting with {0, 0, 1}.
Note: A Tribonacci series is a series in which the next term is the sum of the previous three terms.
Examples:
Input: N = 10
Output: 2 7
Explanation: The first few terms are 0, 0, 1, 1, 2, 4, 7, 13.
Primes in the range [1, 10] are 2 and 7.
Input: N = 2
Output: 2
Naive approach: The simplest approach is to generate a list of Tribonacci numbers in range [1, N] and find the prime numbers in the series which are in the range [1, N].
Time complexity: O(N * √N)
Auxiliary space: O(N)
Efficient Approach: This problem can be solved efficiently based on the following idea:
Generate a list of primes using Sieve of Eratosthenes and then generate Tribonacci number by formula t(n) = t(n-1)+t(n-2)+t(n-3) and find all the prime numbers of the series.
Follow the below steps to implement the above idea:
- Generate a list of primes using Sieve of Eratosthenes.
- Iterate from i = 3 to n (where nth Tribonacci number is at least N):
- Calculate the ith Tribonacci number by formula t(n) = t(n-1)+t(n-2)+t(n-3)
- Check if the t(n) is prime with the help of the already calculated prime numbers.
- Store them in an array (say answer[]).
- Finally print all the elements of the answer[].
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void sieve(vector< bool >& primes, int N)
{
for ( int i = 2; i * i <= N; i++) {
if (primes[i] == true ) {
for ( int j = i + i; j <= N; j = j + i) {
primes[j] = false ;
}
}
}
}
vector< int > findValues( int N)
{
vector< bool > primes(N + 1, true );
primes[0] = false ;
primes[1] = false ;
sieve(primes, N);
vector< int > tribonacci(N + 1);
tribonacci[0] = 0;
tribonacci[1] = 0;
tribonacci[2] = 1;
for ( int i = 3; tribonacci[i - 1] <= N; i++) {
tribonacci[i] = tribonacci[i - 1]
+ tribonacci[i - 2]
+ tribonacci[i - 3];
}
vector< int > ans;
for ( int i = 0; tribonacci[i] <= N; i++) {
int p = tribonacci[i];
if (primes[p] == true ) {
ans.push_back(p);
}
}
return ans;
}
int main()
{
int N = 10;
vector< int > ans = findValues(N);
for ( int i = 0; i < ans.size(); i++) {
cout << ans[i] << " " ;
}
if (ans.size() == 0)
cout << -1;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void sieve( boolean primes[], int N)
{
for ( int i = 2 ; i * i <= N; i++) {
if (primes[i] == true ) {
for ( int j = i + i; j <= N; j = j + i) {
primes[j] = false ;
}
}
}
}
public static ArrayList<Integer> findValues( int N)
{
boolean primes[] = new boolean [N + 1 ];
for ( int i = 0 ; i < N + 1 ; i++) {
primes[i] = true ;
}
primes[ 0 ] = false ;
primes[ 1 ] = false ;
sieve(primes, N);
int tribonacci[] = new int [N + 1 ];
tribonacci[ 0 ] = 0 ;
tribonacci[ 1 ] = 0 ;
tribonacci[ 2 ] = 1 ;
for ( int i = 3 ; tribonacci[i - 1 ] <= N; i++) {
tribonacci[i] = tribonacci[i - 1 ]
+ tribonacci[i - 2 ]
+ tribonacci[i - 3 ];
}
ArrayList<Integer> ans = new ArrayList<>();
for ( int i = 0 ; tribonacci[i] <= N; i++) {
int p = tribonacci[i];
if (primes[p] == true ) {
ans.add(p);
}
}
return ans;
}
public static void main(String[] args)
{
int N = 10 ;
ArrayList<Integer> ans = findValues(N);
for ( int i = 0 ; i < ans.size(); i++) {
System.out.print(ans.get(i) + " " );
}
if (ans.size() == 0 )
System.out.print(- 1 );
}
}
|
Python3
def sieve(primes, N):
i = 2
while (i * i < = N):
if (primes[i] = = True ):
for j in range (i + i, N + 1 , i):
primes[j] = False
i + = 1
def findValues(N):
primes = [ True for i in range (N + 1 )]
primes[ 0 ] = False
primes[ 1 ] = False
sieve(primes, N)
tribonacci = [ 0 for i in range (N + 1 )]
tribonacci[ 0 ] = 0
tribonacci[ 1 ] = 0
tribonacci[ 2 ] = 1
i = 3
while (tribonacci[i - 1 ] < = N):
tribonacci[i] = tribonacci[i - 1 ] + tribonacci[i - 2 ] + tribonacci[i - 3 ]
i + = 1
ans = []
i = 0
while (tribonacci[i] < = N):
p = tribonacci[i]
if (primes[p] = = True ):
ans.append(p)
i + = 1
return ans
N = 10
ans = findValues(N)
for i in range ( len (ans)):
print (f "{ans[i]}" ,end = " " )
if ( len (ans) = = 0 ):
print ( - 1 )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void sieve( bool [] primes, int N)
{
for ( int i = 2; i * i <= N; i++) {
if (primes[i] == true ) {
for ( int j = i + i; j <= N; j = j + i) {
primes[j] = false ;
}
}
}
}
static List< int > findValues( int N)
{
bool [] primes = new bool [N + 1];
for ( int i=0; i<N+1; i++)
{
primes[i] = true ;
}
primes[0] = false ;
primes[1] = false ;
sieve(primes, N);
int [] tribonacci = new int [N + 1];
tribonacci[0] = 0;
tribonacci[1] = 0;
tribonacci[2] = 1;
for ( int i = 3; tribonacci[i - 1] <= N; i++) {
tribonacci[i] = tribonacci[i - 1]
+ tribonacci[i - 2]
+ tribonacci[i - 3];
}
List< int > ans = new List< int >();
for ( int i = 0; tribonacci[i] <= N; i++) {
int p = tribonacci[i];
if (primes[p] == true ) {
ans.Add(p);
}
}
return ans;
}
public static void Main()
{
int N = 10;
List< int > ans = findValues(N);
for ( int i = 0; i < ans.Count; i++) {
Console.Write(ans[i] + " " );
}
if (ans.Count == 0)
Console.Write(-1);
}
}
|
Javascript
<script>
const sieve = (primes, N) => {
for (let i = 2; i * i <= N; i++) {
if (primes[i] == true ) {
for (let j = i + i; j <= N; j = j + i) {
primes[j] = false ;
}
}
}
}
const findValues = (N) => {
let primes = new Array(N + 1).fill( true );
primes[0] = false ;
primes[1] = false ;
sieve(primes, N);
let tribonacci = new Array(N + 1).fill(0);
tribonacci[0] = 0;
tribonacci[1] = 0;
tribonacci[2] = 1;
for (let i = 3; tribonacci[i - 1] <= N; i++) {
tribonacci[i] = tribonacci[i - 1]
+ tribonacci[i - 2]
+ tribonacci[i - 3];
}
let ans = [];
for (let i = 0; tribonacci[i] <= N; i++) {
let p = tribonacci[i];
if (primes[p] == true ) {
ans.push(p);
}
}
return ans;
}
let N = 10;
let ans = findValues(N);
for (let i = 0; i < ans.length; i++) {
document.write(`${ans[i]} `);
}
if (ans.length == 0)
document.write(-1);
</script>
|
Time Complexity: O(N*log(log(N)))
Auxiliary space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...