Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.programming Subject: Re: Here is why C++ and C are bad... Date: Wed, 30 Dec 2015 20:26:41 +0000 Organization: A noiseless patient Spider Lines: 46 Message-ID: <87lh8b65tq.fsf@bsb.me.uk> References: <87h9j06ip1.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="31832"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+r/cvMqhAcImVl/ESNElgXvDx2KEHy1gA=" Cancel-Lock: sha1:HExTqfG8w0+8sUHTGXO52brJsHs= sha1:3wGZ08Vm13qBxoMHGJyvjIa2qtI= X-BSB-Auth: 1.c237da21e6939f0dbdf9.20151230202641GMT.87lh8b65tq.fsf@bsb.me.uk Xref: csiph.com comp.programming:6113 Richard Heathfield writes: > On 30/12/15 17:19, David Brown wrote: > > > >> You didn't get as far as Python, I take it. > > No. I would in any case have chosen PHP for my 'P' language, but from > what you say below it seems that Python would have been the more > interesting choice. > >> In Python, the integer >> types just get as big as you need (until you run out of time or memory). >> So to a large extent you can assume your integers really are >> mathematical integers, and add them as much as you want. (So "unsigned" >> integers don't really exist.) > > What if I wanted to construct a candidate for a large prime? The usual > way to do this is to pick a number of bits, set the top and bottom > bits, and then arbitrarily assign other bits (either setting or > clearing them, as dictated by some entropy source). Then one applies > Miller-Rabin a few times, and adds 2 if any of the tests > fail. Traditionally one uses an array of unsigned char for this, but > if you don't /have/ unsigned char, or indeed any unsigned type, > presumably you could still do this with code such as: > > candidate = 1 << (bitcount - 1); > candidate |= 1; > for i = 1 to bitcount - 2 > candidate |= getrandomnumber(0,1) << i; > > ? Yes (though I'd probably set the bits in larger chucks). In actual Python: candidate = 1 << (bitcount - 1); candidate |= 1; for i in range(1, bitcount - 2): candidate |= random.randint(0,1) << i; > (I realise I'm moving away from signed/unsigned here!) -- Ben.