Open In App

Apocalyptic Number

Last Updated : 23 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a number
// N is Apocalyptic
bool isApocalyptic(int n)
{
    if (to_string(pow(2, n)).find("666") != string::npos)
        return true;
    return false;
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 157;
 
    // Function Call
    if (isApocalyptic(N))
        cout << "Yes";
    else
        cout << "No";
}
 
// This code is contributed by phasing17


Java




// Java program for the above approach
class GFG {
 
    // Function to check if a number
    // N is Apocalyptic
    static boolean isApocalyptic(int n)
    {
        if (String.valueOf((
                               Math.pow(2, n)))
                .contains("666"))
            return true;
        return false;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Number N
        int N = 157;
 
        // Function Call
        if (isApocalyptic(N))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by sapnasingh4991


Python3




# Python3 program for the above approach
 
# Function to check if a number
# N is Apocalyptic 
def isApocalyptic(n):     
    if '666' in str(2**n):
        return True
    return False
 
# Driver Code
 
# Given Number N
N = 157
 
# Function Call
if(isApocalyptic(157)):
    print("Yes")
else:
    print("No")


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if a number
// N is Apocalyptic
static bool isApocalyptic(int n)
{
    if (Math.Pow(2, n).ToString().Contains("666"))
        return true;
         
    return false;
}
 
static public void Main()
{
     
    // Given Number N
    int N = 157;
 
    // Function Call
    if (isApocalyptic(N))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by offbeat


Javascript




<script>
// Javascript implementation
 
// Function to check if N isApocalyptic
function isApocalyptic(n)
{
    var x = Math.pow(2,n);
    if(x.toString().indexOf('666'))
        return true
    return false
}
 
// Driver Code
// Given Number N
var N = 157;
 
// Function Call
if (isApocalyptic(N))
    document.write("Yes");
else
    document.write("No");
   
// This code is contributed by shubhamsingh10
</script>


Output

Yes

Time Complexity: O(n)
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads