📌  相关文章
📜  合适的数字

📅  最后修改于: 2021-04-24 15:42:59             🧑  作者: Mango

给定一个数字N ,任务是检查N是否是一个可接受的数字。如果N是一个可接受的数字,则打印“是”,否则打印“否”

例子:

方法:
前几个可允许编号是1、5、8、9、12、13、16、17、20、21 …..可以表示为4K或4K +1形式。
因此,任何形式为4K或4K +1的数字N都是可接受的数字。
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if N
// is Amenable number
bool isAmenableNum(int N)
{
 
    // Return true if N is of the form
    // 4K or 4K + 1
    return (N % 4 == 0
            || (N - 1) % 4 == 0);
}
 
// Driver Code
int main()
{
    // Given Number
    int n = 8;
 
    // Function Call
    if (isAmenableNum(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if N
// is Amenable number
static boolean isAmenableNum(int N)
{
     
    // Return true if N is of the form
    // 4K or 4K + 1
    return (N % 4 == 0 || (N - 1) % 4 == 0);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 8;
     
    if (isAmenableNum(n))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by shubham


Python3
# Python3 program for the above approach
import math
 
# Function to check if N
# is Amenable number
def isAmenableNum(N):
 
    # Return true if N is of the
    # form 4K or 4K + 1
    return (N % 4 == 0 or
           (N - 1) % 4 == 0);
 
# Driver code
 
# Given number
N = 8;
 
# Function call
if (isAmenableNum(N)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by rock_cool


C#
// C# program for the above approach
using System;
class GFG{
  
// Function to check if N
// is Amenable number
static bool isAmenableNum(int N)
{
      
    // Return true if N is of the form
    // 4K or 4K + 1
    return (N % 4 == 0 || (N - 1) % 4 == 0);
}
  
// Driver code
public static void Main(String[] args)
{
    int n = 8;
      
    if (isAmenableNum(n))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
Yes

时间复杂度: O(1)