Re: [algogeeks] power of 3

2011-01-21 Thread juver++
@manmeet so, please choose your nickname at the forum :) -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@goo

Re: [algogeeks] power of 3

2011-01-21 Thread Manmeet Singh
@ juver++ : fight is my tc handle. here u can call me manmeet :) :) On Fri, Jan 21, 2011 at 9:35 PM, juver++ wrote: > @fight good solution. > 3^n contains only one 1 in the 3-base representation. > So write number M in base-3, and check if it contains only one digit(1). > There is no need to fi

Re: [algogeeks] power of 3

2011-01-21 Thread juver++
@fight good solution. 3^n contains only one 1 in the 3-base representation. So write number M in base-3, and check if it contains only one digit(1). There is no need to find & with M-1. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post

Re: [algogeeks] power of 3

2011-01-21 Thread Manmeet Singh
write n, n-1 to base 3 check if thr & gives 0, if it gives no. is of form 3^n On Fri, Jan 21, 2011 at 8:04 PM, snehal jain wrote: > @manmeet > how? > > > On Fri, Jan 21, 2011 at 7:36 PM, Manmeet Singh wrote: > >> @juver++ : the above is a user defined function. but its possible to the >> problem

Re: [algogeeks] power of 3

2011-01-21 Thread snehal jain
@manmeet how? On Fri, Jan 21, 2011 at 7:36 PM, Manmeet Singh wrote: > @juver++ : the above is a user defined function. but its possible to the > problem using bit wise operators. > > > On Fri, Jan 21, 2011 at 7:29 PM, juver++ wrote: > >> @above it's a user-defined function using fast exponentiat

Re: [algogeeks] power of 3

2011-01-21 Thread Manmeet Singh
@juver++ : the above is a user defined function. but its possible to the problem using bit wise operators. On Fri, Jan 21, 2011 at 7:29 PM, juver++ wrote: > @above it's a user-defined function using fast exponentiation > > -- > You received this message because you are subscribed to the Google G

Re: [algogeeks] power of 3

2011-01-21 Thread juver++
@above it's a user-defined function using fast exponentiation -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr..

Re: [algogeeks] power of 3

2011-01-21 Thread Manmeet Singh
@snehal : YUP On Fri, Jan 21, 2011 at 5:57 PM, abhijith reddy wrote: > @snehal .. misread it .. my apologies. > > > On Fri, Jan 21, 2011 at 5:56 PM, abhijith reddy > wrote: > >> O(lg(n)*lg(n)) is the complexity of the solution ! Not the solution. >> M=3^x >> We binary search on x and then compu

Re: [algogeeks] power of 3

2011-01-21 Thread abhijith reddy
@snehal .. misread it .. my apologies. On Fri, Jan 21, 2011 at 5:56 PM, abhijith reddy wrote: > O(lg(n)*lg(n)) is the complexity of the solution ! Not the solution. > M=3^x > We binary search on x and then compute 3^x in log(x) time using > exponentiation. Hence the complexity. > > > > On Fri, Ja

Re: [algogeeks] power of 3

2011-01-21 Thread abhijith reddy
O(lg(n)*lg(n)) is the complexity of the solution ! Not the solution. M=3^x We binary search on x and then compute 3^x in log(x) time using exponentiation. Hence the complexity. On Fri, Jan 21, 2011 at 5:50 PM, snehal jain wrote: > @juvir++ > it was mentioned in question not to use log or power.

Re: [algogeeks] power of 3

2011-01-21 Thread snehal jain
@juvir++ it was mentioned in question not to use log or power. isnt there any approach using bitwise operators On Fri, Jan 21, 2011 at 5:24 PM, Manmeet Singh wrote: > this will be O(log(n) * log(n)) solution > > > On Fri, Jan 21, 2011 at 4:29 PM, abhijith reddy > wrote: > >> Below is code for mo

Re: [algogeeks] power of 3

2011-01-21 Thread Manmeet Singh
this will be O(log(n) * log(n)) solution On Fri, Jan 21, 2011 at 4:29 PM, abhijith reddy wrote: > Below is code for modular exponentation in general > > // (a^b)%c > int modexp(int a,int b,int c) > { >int ans=1; >while(b) >{ > if(b&1) ans=(ans*a)%c; > a=(a*a)%c; > b>>=1

Re: [algogeeks] power of 3

2011-01-21 Thread abhijith reddy
Below is code for modular exponentation in general // (a^b)%c int modexp(int a,int b,int c) { int ans=1; while(b) { if(b&1) ans=(ans*a)%c; a=(a*a)%c; b>>=1; } return ans; } On Fri, Jan 21, 2011 at 3:27 PM, juver++ wrote: > int l = 0, r = ...; > while (l < r) { >

Re: [algogeeks] power of 3

2011-01-21 Thread juver++
int l = 0, r = ...; while (l < r) { int m = (l + r) / 2; int p = power(3, m); if (p > M) r = m - 1; else if (p < M) l = m + 1; else print 3^m = M; } -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email t

Re: [algogeeks] power of 3

2011-01-21 Thread snehal jain
@ above m nt getting binary search and fast exponentiation .. please elaborate. On Fri, Jan 21, 2011 at 2:07 PM, juver++ wrote: > The most well done solution is to store possible powers of 3 in a table > (this table will be small), and then try to find number M. > > -- > You received this messa

Re: [algogeeks] power of 3

2011-01-21 Thread juver++
The most well done solution is to store possible powers of 3 in a table (this table will be small), and then try to find number M. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.co

Re: [algogeeks] power of 3

2011-01-21 Thread juver++
binary search on the answer and then fast exponentiation. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@go

Re: [algogeeks] power of 3

2011-01-21 Thread Manmeet Singh
Number exponentiation On Fri, Jan 21, 2011 at 1:05 PM, snehal jain wrote: > Given M, find if M = 3^x for some positive integer x efficiently. DO > NOT think of log, pow etc > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to t

[algogeeks] power of 3

2011-01-20 Thread snehal jain
Given M, find if M = 3^x for some positive integer x efficiently. DO NOT think of log, pow etc -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, se