@Arun: Some time ago, there was a question about determining divisibility by 3. I thought about determining divisibility of decimal numbers by 9 using the "casting out nines" procedure: sum the decimal digits of the number; the number is divisible by 9 if the sum of the digits is divisible by 9. Casting out nines works because 9 is one less than the base of the decimal number system. Since we can translate binary numbers to base 4 just by grouping adjacent pairs of bits, I came up with the algorithm for divisibility by 3 that I posted in http://groups.google.com/group/algogeeks/msg/a4894e12805381ca.
To make that algorithm look more like my divisibility by 5 algorithm posted earlier in this string, rewrite it as int DivisibileBy3(int n) { n = n > 0 ? n : -n; while(n>3) n = (n>>2) + (n & 3); return n == 3 || n == 0; } So if divisibility by 3 (= 4-1) is as above, I thought maybe divisibility by 5 (= 4+1) might be related. I tried it and it works! Divisibility by 7: change the 3's above to 7's and the 2 above to 3. Divisibility by 11 would be more difficult. Dave On Jan 21, 11:57 am, Arun Vishwanathan <aaron.nar...@gmail.com> wrote: > @dave: thanks for that..but i just wanted to know as to how u thot of > converting n to a-b in the iteration. when u say 4a +b is a multiple of 5 > iff a-b is a muliple of 5 i was able to get that only when i tried an > example...if they ask divisbility by 3 or 6 or 7 how wud the logic change?? > > On Sat, Jan 21, 2012 at 9:34 AM, karthikeyan muthu < > > > > > > keyankarthi1...@gmail.com> wrote: > > check the last char ... it should be 0 or 5 , int to string without mod > > > On Sat, Jan 21, 2012 at 10:05 PM, Dave <dave_and_da...@juno.com> wrote: > > >> @Karthikeyan: Is this supposed to relate to the question of > >> determining divisibility by 5? > > >> Dave > > >> On Jan 21, 9:25 am, karthikeyan muthu <keyankarthi1...@gmail.com> > >> wrote: > >> > @dave > > >> > int no=10; > >> > char ans[100]; > >> > sprintf(ans,"%d",no); > >> > cout<<ans; > > >> > On Fri, Jan 20, 2012 at 10:29 PM, Arun Vishwanathan > >> > <aaron.nar...@gmail.com>wrote: > > >> > > @dave or anyone: can u pls explain the logic of n&3 in dave's > >> solution? > >> > > why is it subtracted from n(which is divided by 4 using >>2) and what > >> does > >> > > n& 3 indicate? > > >> > > On Sat, May 7, 2011 at 9:38 AM, Dave <dave_and_da...@juno.com> wrote: > > >> > >> @Umer: Do you suppose that you can convert an int into a string > >> > >> without using division or mod, either directly or indirectly? > > >> > >> Dave > > >> > >> On May 4, 1:12 am, Umer Farooq <the.um...@gmail.com> wrote: > >> > >> > I'm surprised to see that why are you guys making this problem so > >> > >> complex. > >> > >> > This problem can be solved in two steps only. > > >> > >> > 1- Convert the given int into string > >> > >> > 2- Check if the last character is 0 or 5. // it yes, then return > >> true > >> > >> else > >> > >> > return false > > >> > >> > for e.g. > > >> > >> > 125 (last character is 5 ... therefore it is divisible by 5) > >> > >> > 120 (last character is 0 ... therefore it is divisible by 5) > >> > >> > 111 (last character is 1 ... therefore it is not divisible by 5) > > >> > >> > The pseudo-code has been written in my above email. > > >> > >> > On Wed, May 4, 2011 at 1:49 AM, Dave <dave_and_da...@juno.com> > >> wrote: > >> > >> > > @anshu: Spoiler alert... I was thinking of something more along > >> the > >> > >> > > line > > >> > >> > > int DivisibleBy5 (int n) > >> > >> > > { > >> > >> > > n = n > 0 ? n : -n; > >> > >> > > while( n > 0 ) > >> > >> > > n = (n >> 2) - (n & 3); > >> > >> > > return (n == 0); > >> > >> > > } > > >> > >> > > To see that it works, write n as n = 4*a + b, where 0 <= b <= 3. > >> Then > >> > >> > > the iteration replaces n by a - b. Consider (4*a + b) + (a - b), > >> the > >> > >> > > sum of two consecutive values of n. This simplifies to 5*a, > >> which is a > >> > >> > > multiple of 5. Thus, n is a multiple of 5 before an iteration if > >> and > >> > >> > > only if it also is a multiple of 5 afterwards, > > >> > >> > > It is clearly log n because n is replaced by a number no greater > >> than > >> > >> > > n/4 on each iteration. > > >> > >> > > Examples: > >> > >> > > n = 125. The sequence of iterates is 30, 5, 0. Ergo, 125 is a > >> multiple > >> > >> > > of 5. > >> > >> > > n = 84. The sequence of iterates is 21, 4, -1. Ergo, 84 is not a > >> > >> > > multiple of 5. > > >> > >> > > Dave > > >> > >> > > On May 3, 3:13 am, anshu <anshumishra6...@gmail.com> wrote: > >> > >> > > > algorithm: > > >> > >> > > > if any number(a) is divisible by 5 it can be wriiten as 4*b + > >> b --> > >> > >> > > > this cleary shows the last two bit of a & b will be same. > > >> > >> > > > lets understand by an example (35)10 = (100011)2 > > >> > >> > > > xx1100 > >> > >> > > > + xx11 > >> > >> > > > --------- > >> > >> > > > 100011 > > >> > >> > > > now this clearly shows we can calculate the unknowns(x) by > >> > >> traversing > >> > >> > > > right to left > > >> > >> > > > code: > > >> > >> > > > int main() > >> > >> > > > { > >> > >> > > > int n, m; > >> > >> > > > cin >> n; > >> > >> > > > m = n; > > >> > >> > > > int a, b; > >> > >> > > > int i=2; > > >> > >> > > > a = (m&3)<<2; > >> > >> > > > b = (m&3); > >> > >> > > > m >>= 2; > > >> > >> > > > bool rem = 0,s,r; > > >> > >> > > > while (m>3) > >> > >> > > > { > >> > >> > > > r = a&(1<<i); > >> > >> > > > s = r^(m&1)^rem; > >> > >> > > > b = b|(s<<i); > >> > >> > > > a = a|(s<<(i+2)); > >> > >> > > > rem = (r&s)|(s&rem)|(r&rem) ; > >> > >> > > > i++; > >> > >> > > > m >>= 1; > >> > >> > > > } > > >> > >> > > > if (a+b == n) cout << "yes\n"; > >> > >> > > > else cout << "no\n"; > > >> > >> > > > return 0; > > >> > >> > > > }- Hide quoted text - > > >> > >> > > > - Show quoted text - > > >> > >> > > -- > >> > >> > > 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. > > >> > >> > -- > >> > >> > Umer- Hide quoted text - > > >> > >> > - Show quoted text - > > >> > >> -- > >> > >> 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. > > >> > > -- > >> > > "People often say that motivation doesn't last. Well, neither does > >> > > bathing - that's why we recommend it daily." > > >> > > -- > >> > > 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. > > > -- > > 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. > > -- > "People often say that motivation doesn't last. Well, neither does bathing > - that's why we recommend it daily." -- 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.