0

Please help me find the time-complexity of below recursive algo. I have guessed for first two return statements.Please correct me if I am wrong. But I can't find time-complexity of third return statement because it will loop till we get a factorial of NUM contining 0 and 7.

    public class Check
   {

   static long NUM = 11;
   public static void main(String[] args)
   {

       System.out.print(find007(NUM));
   }

     static long find007(long n){

       if(is007(n))  //O(1)
         return n;

       if(n+NUM<n)   //O(1)
         return 0;

       return find007(n+NUM);  //O(?)
     }
     static boolean is007(long n){
        while(n!=0 && (n%10==0 || n%10==7))  //O(log n)
         n=n/10;

       return n==0;
     }
}

Pseudo-code:

static long find007(long n){

 check if number or its multiple consists of only 0 or 7.
if yes, return the answer/number
 return n;

//if not, keep checking for the other multiples of NUM
i.e n+NUM (here NUM=11, so it will keep adding 11 to itself till 77)
   return find007(n+NUM);
}

 static boolean is007(long n){

   continue till n!=0 and  n%10==0 OR n%10==7
        n=n/10;

     return true if n==0;         

         }
Raphael
  • 72,336
  • 29
  • 179
  • 389
Ankita
  • 1
  • 2
  • @DavidRicherby : Thank you for sharing the link. But it involves tough mathematics and I am a non-CS grad and newbie to algorthms. Maybe you could explain in simpler language. I know the master-theorem but unable to apply it here. – Ankita Mar 26 '17 at 12:57
  • Can you replace the C++/Java code with pseudocode? – Yuval Filmus Mar 26 '17 at 13:18
  • @YuvalFilmus: edited the question and added the pseudocode. Hope it is understandable – Ankita Mar 26 '17 at 13:30
  • Welcome to Computer Science! What have you tried? Where did you get stuck? We do not want to just hand you the solution; we want you to gain understanding. However, as it is we do not know what your underlying problem is, so we can not begin to help. See here for tips on asking questions about exercise problems. If you are uncertain how to improve your question, why not ask around in [chat]? – Raphael Mar 26 '17 at 13:37
  • Algorithms analysis without mathematics is like structural analysis without mathematics: ultimately impossible. You will either have to learn some amount of mathematics (not much more than high-school level, in my estimation) or resort to other means of estimating your code's performance. – Raphael Mar 26 '17 at 13:38
  • Thank you @Raphael for your valuable suggestions. I will keep that in mind next time before asking question here – Ankita Mar 26 '17 at 15:19
  • @Raphael: I will explain you where I am stuck in this question. Suppose if here in the recursion the return statement was return fun(n-1) then the complexity would be O(n) as function will execute n times or if it was return fun(n-5) then also it will execute O(n-5)~O(n). But here we do not know when it will find the multiple of the number which contains just 0 or 7, so how many times will it execute? Your help is appreciated. Thanks in advance – Ankita Mar 26 '17 at 15:20
  • @Ankita "it will execute O(n-5)" -- wrong. It seems you are lacking basic skills in solving recurrences (which has nothing to do with algorithm analysis per se); our reference question may help. – Raphael Mar 26 '17 at 15:31
  • In return find007(n+NUM); ,we don't know when we will find the required multiple – Ankita Mar 26 '17 at 15:31
  • @Raphael: Thanks again for help. O(n-5) will be a tail-recursive as it goes from n to 0. And the return find007(n+NUM); will be of the type T(n)=T(n−1)+f(n), where f(n) is monotone increasing as n+NUM is increasing. Please correct me if I am wrong – Ankita Mar 26 '17 at 15:49

0 Answers0