Check if a number is Flavius Number
Given a series of integers from 1 to infinity and a number N.
The task is to remove every (i + 1)-th element from the remaining series at every i-th iteration and find whether the given number N exists in the series or not.
Flavius Number:
Numbers in the Flavius Sieve are called Flavius Numbers.
Flavius sieve starts with the natural numbers and keep repeating the below step:
At the k-th sieving step, remove every (k+1)-st term of the sequence remaining of N natural numbers after the (k-1)-st sieving step.
For Example: 1, 3, 7, 13, 19, 27, 39, 49,
Examples:
Input: N = 17
Output: N0
Series after i-th iterations
1). 1, 3, 5, 7, 9, 11, 13, 15, 17, …
2). 1, 3, 7, 9, 13, 15, 19, 21, 25, …
3). 1, 3, 7, 13, 15, 19, 25, …
4). 1, 3, 7, 13, 19, 27, ….
Input: N = 3
Output: Yes
Approach:
- If the given number is even so the answer is simply “No”. Because in the first iteration all the even numbers has been eliminated from the series.
- Repeat this process.
- Else remove the number of elements removed at 1st iteration i.e. (1/2)th the number and then check
if it is divisible by 3 the answer should be “No”, else subtract the numbers before it that were
removed i.e. (1/3)rd the number and so on.
- Repeat the above step for all iterations until we get an answer.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool Survives( int n)
{
int i;
for ( int i = 2;; i++) {
if (i > n)
return true ;
if (n % i == 0)
return false ;
n -= n / i;
}
}
int main()
{
int n = 17;
if (Survives(n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
class GFG
{
static boolean Survives( int n)
{
for ( int i = 2 ;; i++)
{
if (i > n)
return true ;
if (n % i == 0 )
return false ;
n -= n / i;
}
}
public static void main(String[] args)
{
int n = 17 ;
if (Survives(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def Survives(n) :
i = 2
while ( True ) :
if (i > n) :
return True ;
if (n % i = = 0 ) :
return False ;
n - = n / / i;
i + = 1
if __name__ = = "__main__" :
n = 17 ;
if (Survives(n)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static bool Survives( int n)
{
for ( int i = 2;; i++)
{
if (i > n)
return true ;
if (n % i == 0)
return false ;
n -= n / i;
}
}
public static void Main(String[] args)
{
int n = 17;
if (Survives(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function Survives(n)
{
let i;
for (let i = 2;; i++) {
if (i > n)
return true ;
if (n % i == 0)
return false ;
n -= parseInt(n / i);
}
}
let n = 17;
if (Survives(n))
document.write( "Yes<br>" );
else
document.write( "No<br>" );
</script>
|
Time complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
31 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...