给定数字N ,任务是检查N是否为世界末日数。如果N是一个世界末日数,则打印“是”,否则打印“否” 。
Apocalyptic Number is a number such that 2N contains “666” as the substring.
例子:
Input: N = 157
Output: Yes
Explanation:
2157 = 182687704666362864775460604089535377456991567872
which contains as the substring “666”.
Input: N = 10
Output: No
方法:想法是计算2 N的值,并检查结果数字是否包含“ 666”作为子字符串。如果它以“ 666”作为子字符串,则打印“是”,否则打印“否” 。
下面是上述方法的实现:
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
输出
Yes
时间复杂度: O(n)