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;
}
return fun(n-1)
then the complexity would be O(n) as function will execute n times or if it wasreturn 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:20return find007(n+NUM);
,we don't know when we will find the required multiple – Ankita Mar 26 '17 at 15:31return 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