Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Algorithm writing tips?
#7
KajitiSouls Wrote:That's a pattern that's been eluding me all this time xD Your parenthetical statement seems to be in conflict though...

Having seen that, I'm smacking myself in the head for such a stupid oversight. I've come to the conclusion that I should only check for input % n, where n is a prime number. I'm not familiar with the code (n & 1), but I understand what you mean.

Oyas! That's why I use the debugger (whatever works) and check each section of code that I write in a progressive sequence, and I ask the computer for an appropriate output to make sure that my code works before moving on. Personally my worst enemies are recursions methods.

One genius thing you did here. Colourizing. Too bad I'm not going to bother to do the same, sorry Redface

Sorry, I've been using lisp too much, that may be why there are too many parentheses.

I've been somewhat unfamiliar with how Java works, but I'm quite sure it can be written as (n & 1 == 1).
With that being said, I'll try to explain it quite short. For a human being, to calculate this, we would have to convert n and 1 into bitcode. Let's say n equals 23:

Base 10Base 2
100001
2310111


Now, let's see how & reacts

00001
&10111
=00001


Though it may look completely in-understandable, it's the bitwise &-operator. If two bits at same position equals 1, then the bit at that position is set as 1. Else, it is set as 0. Another example:

1011010101110001001
&0010111101010111010
=0010010101010001000


It can be proven that any given number n is a number divisible by 2 if (n & 1 == 0).
Compared to base 10, it's like saying that any number n is divisible by 10 if the last digit in the number is 0.
Reply


Messages In This Thread
Algorithm writing tips? - by KajitiSouls - 2009-01-11, 10:40 PM
Algorithm writing tips? - by Stereo - 2009-01-11, 10:58 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-11, 11:36 PM
Algorithm writing tips? - by Nikkey - 2009-01-12, 09:58 AM
Algorithm writing tips? - by Stereo - 2009-01-12, 11:52 AM
Algorithm writing tips? - by KajitiSouls - 2009-01-12, 03:53 PM
Algorithm writing tips? - by Nikkey - 2009-01-12, 04:19 PM
Algorithm writing tips? - by Stereo - 2009-01-12, 04:50 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-12, 05:25 PM
Algorithm writing tips? - by Nikkey - 2009-01-12, 05:59 PM
Algorithm writing tips? - by GummyBear - 2009-01-12, 08:13 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-12, 10:26 PM
Algorithm writing tips? - by Nikkey - 2009-01-13, 10:28 AM
Algorithm writing tips? - by GummyBear - 2009-01-13, 11:41 PM
Algorithm writing tips? - by Stereo - 2009-01-15, 10:38 AM
Algorithm writing tips? - by Nikkey - 2009-01-15, 11:02 AM
Algorithm writing tips? - by Kiley - 2009-01-16, 02:37 AM
Algorithm writing tips? - by KajitiSouls - 2009-01-18, 01:48 AM
Algorithm writing tips? - by Stereo - 2009-01-18, 04:46 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-18, 05:37 PM
Algorithm writing tips? - by Nikkey - 2009-01-18, 06:26 PM
Algorithm writing tips? - by Stereo - 2009-01-18, 08:02 PM
Algorithm writing tips? - by KajitiSouls - 2009-01-18, 08:29 PM
Algorithm writing tips? - by Stereo - 2009-01-18, 10:45 PM
Algorithm writing tips? - by Nikkey - 2009-01-19, 02:10 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)