📜  幸运数字

📅  最后修改于: 2021-05-06 18:31:27             🧑  作者: Mango

幸运数字是整数的子集。与其深入讨论理论,不如让我们看看得出幸运数字的过程,
取整数集
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,……
首先,删除第二个数字,得到以下简化集合。
1,3,5,7,9,11,13,15,17,19,…………
现在,删除第三个数字,我们得到
1、3、7、9、13、15、19,……..。
无限期继续此过程……
由于上述过程而不会被删除的任何数字都称为“幸运”。
因此,幸运数字的集合是1、3、7、13 ………
现在,给定整数“ n”,编写一个函数来说明该数字是否很幸运。

bool isLucky(int n)

算法:
在每次迭代之前,如果我们计算给定数字的位置,那么在给定迭代中,我们可以确定是否删除该数字。假设给定数字的计算位置在某个迭代之前为P,并且在此迭代中将删除每个第I个数字,即

f P <我输入的数字很幸运,

如果P使得P%I == 0(I是P的除数),则输入no是不幸运的。

如何计算数字的下一个位置:

我们知道,最初数字的位置本身就是第n个。现在,任何下一个位置将等于前一个位置减去已删除元素(或说项目)的数量。

也就是说, next_position = current_position –删除的数量计数

例如,以n = 13的情况为例。

我们有:初始位置:n,即。 13本身。

1,2,3,4,5,6,7,8,9,10,11,12,13

现在,在删除第二个元素之后,我们实际上删除了n / 2个元素。所以现在13的位置将是: nn / 2 = 13-6 = 7(n = 13),i = 2

1,3,5,7,9,11,13。

之后,我们删除n / 3个项目。注意,n现在是n = 7。因此位置13: nn / 3 = 7-7 / 3 = 7-2 = 5(n = 7),i = 3

1,3,7,9,13

因此下一个将是: nn / 4 = 5-5 / 4 = 4(n = 5),i = 4

1,3,7,13

因此,现在i = 5,但由于位置13仅是4,因此将保存该位置。因此,一个幸运的数字! n = 4,i = 5

递归方式:

C++
// C++ program for Lucky Numbers
#include 
using namespace std;
#define bool int
 
/* Returns 1 if n is a lucky no.
otherwise returns 0*/
bool isLucky(int n)
{
    static int counter = 2;
     
    /*variable next_position is just for
    readability of the program we can
    remove it and use n only */
    int next_position = n;
    if(counter > n)
        return 1;
    if(n % counter == 0)
        return 0;
     
    /*calculate next position of input no*/
    next_position -= next_position / counter;
     
    counter++;
    return isLucky(next_position);
}
 
// Driver Code
int main()
{
    int x = 5;
    if( isLucky(x) )
        cout << x << " is a lucky no.";
    else
        cout << x << " is not a lucky no.";
}
 
// This code is contributed
// by rathbhupendra


C
#include 
#define bool int
 
/* Returns 1 if n is a lucky no. ohterwise returns 0*/
bool isLucky(int n)
{
    static int counter = 2;
     
    /*variable next_position is just for readability of
        the program we can remove it and use n only */
    int next_position = n;
    if(counter > n)
        return 1;
    if(n%counter == 0)
        return 0;    
     
    /*calculate next position of input no*/
    next_position -= next_position/counter;
     
    counter++;
    return isLucky(next_position);
}
 
/*Driver function to test above function*/
int main()
{
    int x = 5;
    if( isLucky(x) )
        printf("%d is a lucky no.", x);
    else
        printf("%d is not a lucky no.", x);
    getchar();
}


Java
// Java program to check Lucky Number
import java.io.*;
 
class GFG
{
    public static int counter = 2;   
 
    // Returns 1 if n is a lucky no.
    // ohterwise returns 0
    static boolean isLucky(int n)
    {
        // variable next_position is just
        // for readability of
        // the program we can remove it
        // and use n only
        int next_position = n;
        if(counter > n)
            return true;
        if(n%counter == 0)
            return false;     
  
        // calculate next position of input no
        next_position -= next_position/counter;
    
        counter++;
        return isLucky(next_position);
    }
     
    // driver program
    public static void main (String[] args)
    {
        int x = 5;
        if( isLucky(x) )
            System.out.println(x+" is a lucky no.");
        else
            System.out.println(x+" is not a lucky no.");
    }
}
 
// Contributed by Pramod Kumar


Python
# Python program to check for lucky number
 
# Returns 1 if n is a lucky number
# otherwise returns 0
def isLucky(n):
     
    # Function attribute will act
    # as static variable  
    # just for readability, can be
    # removed and used n instead
    next_position = n
     
    if isLucky.counter > n:
        return 1
    if n % isLucky.counter == 0:
        return 0
     
    # Calculate next position of input number
    next_position = next_position - next_position /
                                   isLucky.counter
     
    isLucky.counter = isLucky.counter + 1
     
    return isLucky(next_position)
     
     
# Driver Code
 
isLucky.counter = 2 # Acts as static variable
x = 5
if isLucky(x):
    print x,"is a Lucky number"
else:
    print x,"is not a Lucky number"
     
# Contributed by Harshit Agrawal


C#
// C# program to check Lucky Number
using System;
 
class GFG {
     
    public static int counter = 2;
 
    // Returns 1 if n is a lucky no.
    // ohterwise returns 0
    static bool isLucky(int n)
    {
         
        // variable next_position is
        // just for readability of
        // the program we can remove
        // it and use n only
        int next_position = n;
         
        if(counter > n)
            return true;
        if(n % counter == 0)
            return false;    
 
        // calculate next position of
        // input no
        next_position -= next_position
                            / counter;
     
        counter++;
         
        return isLucky(next_position);
    }
     
    // driver program
    public static void Main ()
    {
        int x = 5;
         
        if( isLucky(x) )
            Console.Write(x + " is a "
                         + "lucky no.");
        else
            Console.Write(x + " is not"
                      + " a lucky no.");
    }
}
 
// This code is contributed by
// nitin mittal.


PHP
 $n)
        return 1;
    if($n % $counter == 0)
        return 0;
     
    /* calculate next position
       of input no */
    $next_position -= $next_position / $counter;
     
    $counter++;
    return isLucky($next_position);
}
 
    // Driver Code
    $x = 5;
    if(isLucky($x) )
        echo $x ," is a lucky no.";
    else
        echo $x ," is not a lucky no.";
         
// This code is contributed by anuj_67.
?>


Javascript


输出:

5 is not a lucky no.

例子:
让我们以19为例
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,15,17,18,19,20,21,……
1,3,5,7,9,11,13,15,17,19,…..
1,3,7,9,13,15,19,………。
1,3,7,13,15,19,………
1,3,7,13,19,………
在下一步中,将删除每第6个按顺序排列的.in。在此步骤之后,不会删除19,因为在此步骤之后19的位置是第5位。因此,19是幸运的。让我们看看上面的C代码是如何发现的:

这个呼叫计数器为下一个呼叫电流函数调用位置接着呼叫isLucky(19)10 3 isLucky(10)isLucky(10)7 4 isLucky(7)isLucky(7)6 5 isLucky(6)isLucky(6)5 6 isLucky (5)