both will work fine in first case in each step what is done is ans = (ans*(n+1-i))/i; what happens is
first iteration ans = n, i =1 so i divides ans sec. iteration ans = product of 2 consecutive nos at least one even so divisible by 2 in third, ans is prod of 3 consecutive no.s so divisible by 3 and so on On Tue, Jun 21, 2011 at 9:15 PM, Wladimir Tavares <wladimir...@gmail.com> wrote: > > I got a doubt in the following code: > > if (k> n / 2) > k = n-k; > for (i = 1; i <= k, i + +) > ans = ans * (n +1- i) / i; > > If (n +1- i) is not divisible by i this code should be wrong? > > I thought I needed to keep the numerator and denominator and go by dividing the numerator and denominator gcd > > num = 1 > den = 1 > > for (i = 1; i <= k, i + +) { > num *= n +1- i; > den *= i; > d = gcd (num, den); > num /= d; > den / = d; > } > > printf ("% d", num); > > Wladimir Araujo Tavares > Federal University of CearĂ¡ > > > > > > > On Tue, Jun 21, 2011 at 12:34 PM, PRAMENDRA RATHi rathi < prathi...@gmail.com> wrote: >> >> i have also got AC...i am only asking for best algo..... >> >> >> >> >> >> >> PRAMENDRA RATHI >> NIT ALLAHABAD >> >> >> >> -- >> 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...@googlegroups.com. >> For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. > > -- > 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...@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- 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...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.