Apocalyptic Number
Last Updated :
23 Sep, 2022
Given a number N, the task is to check if N is an Apocalyptic Number or not. If N is an Apocalyptic Number then print “Yes” else print “No”.
Apocalyptic Number is a number such that 2N contains “666” as the substring.
Examples:
Input: N = 157
Output: Yes
Explanation:
2157 = 182687704666362864775460604089535377456991567872
which contains as the substring “666”.
Input: N = 10
Output: No
Approach: The idea is to calculate the value of 2N and check if the resultant number contains “666” as a substring or not. If it has “666” as a substring then print “Yes” else print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isApocalyptic( int n)
{
if (to_string( pow (2, n)).find( "666" ) != string::npos)
return true ;
return false ;
}
int main()
{
int N = 157;
if (isApocalyptic(N))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
class GFG {
static boolean isApocalyptic( int n)
{
if (String.valueOf((
Math.pow( 2 , n)))
.contains( "666" ))
return true ;
return false ;
}
public static void main(String[] args)
{
int N = 157 ;
if (isApocalyptic(N))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isApocalyptic(n):
if '666' in str ( 2 * * n):
return True
return False
N = 157
if (isApocalyptic( 157 )):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isApocalyptic( int n)
{
if (Math.Pow(2, n).ToString().Contains( "666" ))
return true ;
return false ;
}
static public void Main()
{
int N = 157;
if (isApocalyptic(N))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isApocalyptic(n)
{
var x = Math.pow(2,n);
if (x.toString().indexOf( '666' ))
return true
return false
}
var N = 157;
if (isApocalyptic(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...