Lucky Numbers

Sort by

recency

|

29 Discussions

|

  • + 0 comments

    Quite easy using recursion: import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

    public static boolean lucky_check(long num){
        if(num<4 && num!=0){
            return false;
        }
        if(num%7==0 || num%4==0){
            return true;
        }
        else{
    
            boolean check1 =  lucky_check(num-7);
            boolean check2 =  lucky_check(num-4);
    
            if(check1 || check2){
                return true;
            }
            else{
                return false;
            }
    
        }
    
    
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            long n = in.nextLong();
            if(lucky_check(n)){
                System.out.println("Yes");
            }
            else{
                 System.out.println("No");
            }
        }
    }
    

    }

  • + 0 comments

    Use modulus. Psudo code: if n % 4, 7, 11 != 0: print("No") else: print("yes")

  • + 0 comments
    #include <map>
    #include <set>
    #include <list>
    #include <cmath>
    #include <ctime>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <string>
    #include <bitset>
    #include <cstdio>
    #include <limits>
    #include <vector>
    #include <climits>
    #include <cstring>
    #include <cstdlib>
    #include <fstream>
    #include <numeric>
    #include <sstream>
    #include <iostream>
    #include <algorithm>
    #include <unordered_map>
    
    using namespace std;
    
    
    int main(){
        int q;
        cin >> q;
        for(int a0 = 0; a0 < q; a0++){
            long n,x=0,i=0;
            cin >> n;
            while(n>=7*i)
                {
                if(((n-7*i)%4)==0)
                    {
                    x=1;
                    cout<<"Yes"<<endl;
                    break;
                }
                i++;
            }
            if(x==0)
                cout<<"No"<<endl;
        }
        return 0;
    }
    
  • + 0 comments

    what's the issue with this code :

    def isdivi(num):
        if num <0 :
            return False 
        if num == 0:
            return True
        
        return (isdivi(n-7)  or isdivi(n-4))
    
    q = int(raw_input().strip())
    for a0 in xrange(q):
        n = long(raw_input().strip())
        if (isdivi(n)):
            print "Yes"
        else :
            print "No"
    
  • + 0 comments

    We have

    7(x-4)+4(y+7)=7x+4y

    So if (x, y) is a solution, then (x-4,y+7) is also a solution. Hence if there is a solution then there is one with x<4. That's why you only need to test x=0..3 which runs in constant time.

    This can be extended to any equation of the form ax+by=n, you only need to test x=0..b-1.