📜  检查数字是否是弗拉维乌斯数

📅  最后修改于: 2021-10-23 07:34:34             🧑  作者: Mango

给定一系列从1无穷大的整数和一个数字N
任务是在每第i 次迭代时从剩余序列中删除每个(i + 1)-th元素,并找出给定数字N 是否存在于该序列中。
弗拉维乌斯数

例子:

方法:

  • 如果给定的数字是偶数,那么答案就是“否”。因为在第一次迭代中,所有偶数都从系列中消除了。
  • 重复这个过程。
    • 否则删除在第一次迭代时删除的元素数量,即 (1/2)th 数量,然后检查
      如果它可以被 3 整除,答案应该是“否”,否则减去它之前的数字
      删除即 (1/3)rd 数字等。
    • 对所有迭代重复上述步骤,直到我们得到答案。

下面是该方法的实现:

C++
// C++ implementation
#include 
using namespace std;
 
// Return the number is
// Flavious Number or not
bool Survives(int n)
{
    int i;
 
    // index i starts from 2 because
    // at 1st iteration every 2nd
    // element was remove and keep
    // going for k-th iteration
    for (int i = 2;; i++) {
        if (i > n)
            return true;
        if (n % i == 0)
            return false;
 
        // removing the elements which are
        // already removed at kth iteration
        n -= n / i;
    }
}
 
// Driver Code
int main()
{
    int n = 17;
    if (Survives(n))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG
{
 
// Return the number is
// Flavious Number or not
static boolean Survives(int n)
{
 
    // index i starts from 2 because
    // at 1st iteration every 2nd
    // element was remove and keep
    // going for k-th iteration
    for (int i = 2;; i++)
    {
        if (i > n)
            return true;
        if (n % i == 0)
            return false;
 
        // removing the elements which are
        // already removed at kth iteration
        n -= n / i;
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 17;
    if (Survives(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of
# the above approach
 
# Return the number is
# Flavious Number or not
def Survives(n) :
 
    # index i starts from 2 because
    # at 1st iteration every 2nd
    # element was remove and keep
    # going for k-th iteration
    i = 2
    while(True) :
         
        if (i > n) :
            return True;
             
        if (n % i == 0) :
            return False;
 
        # removing the elements which are
        # already removed at kth iteration
        n -= n // i;
        i += 1
 
# Driver Code
if __name__ == "__main__" :
 
    n = 17;
     
    if (Survives(n)) :
        print("Yes");
         
    else :
        print("No");
         
# This code is contributed by AnkitRai01


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
// Return the number is
// Flavious Number or not
static bool Survives(int n)
{
 
    // index i starts from 2 because
    // at 1st iteration every 2nd
    // element was remove and keep
    // going for k-th iteration
    for (int i = 2;; i++)
    {
        if (i > n)
            return true;
        if (n % i == 0)
            return false;
 
        // removing the elements which are
        // already removed at kth iteration
        n -= n / i;
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 17;
    if (Survives(n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
No

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程