modulus operator

Started by
17 comments, last by JotaKa 20 years, 1 month ago
quote:Original post by Oluseyi
For large collections of divisibility (such as the computation of primes), using methods similar to the Sieve of Eratosthenes is better: Create an indexable bitset (std::bitset or std::bitvector) ...
I''d tend to use a std::valarray<bool> for this purpose. std::slice can come in handy.
Advertisement
from numarray import *def primes(maxnum):    n = arange(2,maxnum)    p = [1]    while(numbers):        p.append(n[0])        n = compress( n%p[-1] != 0, n)    return p  

Probably not the most efficient, I know.

Alternatively:
def iprime():   yield 1   yield 2   p = array([2])   n = 3   while(1):      if alltrue(n%p):         p = concatenate([p,[n]])         yield n      n=n+1  

spits out primes on demand.

Note - In my books, 1 is considered as prime, and a prime number is a number which has exactly two divisors, 1 and itself.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on February 27, 2004 7:02:14 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Someone showed you an alternative using an ampersand (&). The way that works is, if you''re doing this:

X % Y

and Y is a power of 2, you can rewrite it as:

X & (Y-1)

And that''s a lot faster than using modulus. So to take the remainder of X divided by 256, you do:

R = X & 255;

which people sometimes write as:

R = X & 0xFF;

You may have seen, or will see, bits of code like that. That''s what they''re doing.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
quote:Original post by Fruny
quote:Original post by Kurioes
EDIT: please note 0 is not divisible by two as 0 is not in 1, 2, 3, ... (N);


Zero is divisible by 2.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)


not by my definition
quote:Original post by Kurioes
quote:Original post by Fruny
quote:Original post by Kurioes
EDIT: please note 0 is not divisible by two as 0 is not in 1, 2, 3, ... (N);


Zero is divisible by 2.


not by my definition


An integer K is divisible by 2 if you can write it as 2P for some integer P. Obviously, you can write 0 in the form 2P by letting P = 0.

What silly definition are you using?

quote:Original post by adamg

What silly definition are you using?


Just the one that P must be in N. Now that I think of it it probably should be Z

Sorry, just ignore me :-/
oh no! I see a potentially long thread looming on the horizon because of a discrepancy on the number 0, this time as the dividend. God forbid 0 would be the divisor and then the fabric of the universe is torn in two!
Well, R2D22U2..
quote:Original post by Kurioes
Just the one that P must be in N. Now that I think of it it probably should be Z


There is no consensus either as to whether 0 is in N or not.

I learned math with 0∈N and N *=N-{0}.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)


[edited by - Fruny on February 28, 2004 8:27:53 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ah well... I use both My teachers mix them up as they please. (That''s why I did)


[ Galactic Conquest | Bananas | My dead site | www.sgi.com | Goegel ]

This topic is closed to new replies.

Advertisement