Re: division by 7 efficiently ???

2007-02-07 Thread Roel Schroeven
[EMAIL PROTECTED] schreef: On Feb 6, 4:54 pm, John Machin [EMAIL PROTECTED] wrote: Recursive? Bzzzt! Might it not be better to halve the interval at each iteration instead of calling a random number function? mid = (lo + hi) 1 looks permitted and cheap to me. Also you don't run the risk of

Re: division by 7 efficiently ???

2007-02-07 Thread Roel Schroeven
Roel Schroeven schreef: [EMAIL PROTECTED] schreef: I had considered this, but to halve, you need to divide by 2. Using random, while potentially increasing the number of iterations, removes the dependency of language tricks and division. It's possible to use Fibonacci numbers instead of

Re: division by 7 efficiently ???

2007-02-06 Thread garrickp
On Feb 1, 8:25 pm, Krypto [EMAIL PROTECTED] wrote: The correct answer as told to me by a person is (N3) + ((N-7*(N3))3) The above term always gives division by 7 Does anybody else notice that this breaks the spirit of the problem (regardless of it's accuracy)? 'N-7' uses the subtraction

Re: division by 7 efficiently ???

2007-02-06 Thread MRAB
On Feb 6, 3:29 pm, [EMAIL PROTECTED] wrote: On Feb 1, 8:25 pm, Krypto [EMAIL PROTECTED] wrote: The correct answer as told to me by a person is (N3) + ((N-7*(N3))3) The above term always gives division by 7 Does anybody else notice that this breaks the spirit of the problem (regardless

Re: division by 7 efficiently ???

2007-02-06 Thread John Machin
On Feb 7, 2:29 am, [EMAIL PROTECTED] wrote: On Feb 1, 8:25 pm, Krypto [EMAIL PROTECTED] wrote: The correct answer as told to me by a person is (N3) + ((N-7*(N3))3) The above term always gives division by 7 Does anybody else notice that this breaks the spirit of the problem (regardless

Re: division by 7 efficiently ???

2007-02-06 Thread garrickp
On Feb 6, 4:54 pm, John Machin [EMAIL PROTECTED] wrote: Recursive? Bzzzt! I woudl be happy to hear your alternative, which doesn't depend on language specific tricks. Thus far, all you have suggested is using an alternative form of the division function, which I would consider to be outside the

Re: division by 7 efficiently ???

2007-02-06 Thread John Machin
On Feb 7, 11:05 am, [EMAIL PROTECTED] wrote: On Feb 6, 4:54 pm, John Machin [EMAIL PROTECTED] wrote: Recursive? Bzzzt! I woudl be happy to hear your alternative, which doesn't depend on language specific tricks. Thus far, all you have suggested is using an alternative form of the division

Re: division by 7 efficiently ???

2007-02-02 Thread Bart Ogryczak
On Feb 1, 3:42 am, [EMAIL PROTECTED] wrote: How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer. It´s quiet simple. x == 8*(x/8) + x%8, so x == 7*(x/8) + (x/8 + x%8)

Re: division by 7 efficiently ???

2007-02-02 Thread Neil Cerutti
On 2007-02-01, Michael Yanowitz [EMAIL PROTECTED] wrote: I think it is off by 1 in small numbers, off by a little more with large numbers: def div7 (N): ...return (N3) + ((N-7*(N3))3) ... div7 (70) 9 div7 (77) 10 div7 (700) 98 div7 (7) 0 div7 (10) 1 div7 (14) 1 div7 (21) 2

Re: division by 7 efficiently ???

2007-02-02 Thread Jesse Chounard
On 31 Jan 2007 19:13:14 -0800, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Its not an homework. I appeared for EA sports interview last month. I was asked this question and I got it wrong. I have already fidlled around with the answer but I don't know the correct reasoning behind it. I think

Re: division by 7 efficiently ???

2007-02-02 Thread John Machin
On Feb 2, 11:03 pm, Bart Ogryczak [EMAIL PROTECTED] wrote: On Feb 1, 3:42 am, [EMAIL PROTECTED] wrote: How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer. It´s

Re: division by 7 efficiently ???

2007-02-02 Thread John Machin
On Feb 3, 2:31 am, Jesse Chounard [EMAIL PROTECTED] wrote: On 31 Jan 2007 19:13:14 -0800, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Its not an homework. I appeared for EA sports interview last month. I was asked this question and I got it wrong. I have already fidlled around with the

Re: division by 7 efficiently ???

2007-02-02 Thread Bart Ogryczak
On Feb 1, 2:00 pm, Nicko [EMAIL PROTECTED] wrote: precision and the answer that they were looking for was: a = (b * 045L) 32 Note that the constant there is in octal. 045L? Shouldn´t it be 044? Or more generally, const = (1bitPrecision)/7 a = (b *

Re: division by 7 efficiently ???

2007-02-02 Thread Nicko
On Feb 2, 4:21 pm, Bart Ogryczak [EMAIL PROTECTED] wrote: On Feb 1, 2:00 pm, Nicko [EMAIL PROTECTED] wrote: precision and the answer that they were looking for was: a = (b * 045L) 32 Note that the constant there is in octal. 045L? Shouldn´t it be 044? Or

Re: division by 7 efficiently ???

2007-02-02 Thread Nicko
On Feb 1, 8:25 pm, Krypto [EMAIL PROTECTED] wrote: The correct answer as told to me by a person is (N3) + ((N-7*(N3))3) The above term always gives division by 7 No it doesn't. The above term tends towards N * (9/64), with some significant rounding errors. 9/64 is a fairly poor (6 bit)

Re: division by 7 efficiently ???

2007-02-02 Thread Garrick . Peterson
How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer. It may not be efficient, but the easiest solution (without using tricks from a particular languages) is to increment a

Re: division by 7 efficiently ???

2007-02-01 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], Paul McGuire wrote: On Jan 31, 11:36 pm, Paddy [EMAIL PROTECTED] wrote: On Feb 1, 2:42 am, [EMAIL PROTECTED] wrote: How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I

Re: division by 7 efficiently ???

2007-02-01 Thread BJörn Lindqvist
This is maybe not so efficient :) but it implements integer division by 7 for positive integers without - and /. def div2(num): return num 1 def div4(num): return num 2 def div8(num): return num 3 def mul2(num): return num 1 def mul4(num): return num 2 def mul7(num):

Re: division by 7 efficiently ???

2007-02-01 Thread Nicko
On Feb 1, 3:13 am, [EMAIL PROTECTED] wrote: Its not an homework. I appeared for EA sports interview last month. I was asked this question and I got it wrong. I have already fidlled around with the answer but I don't know the correct reasoning behind it. In that case, observer that a/b == a *

Re: division by 7 efficiently ???

2007-02-01 Thread Krypto
The correct answer as told to me by a person is (N3) + ((N-7*(N3))3) The above term always gives division by 7 -- http://mail.python.org/mailman/listinfo/python-list

Re: division by 7 efficiently ???

2007-02-01 Thread Paul Rubin
Krypto [EMAIL PROTECTED] writes: The correct answer as told to me by a person is (N3) + ((N-7*(N3))3) The above term always gives division by 7 Well, not exactly: [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2 Type help, copyright, credits or license for more information.

RE: division by 7 efficiently ???

2007-02-01 Thread Michael Yanowitz
Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Krypto Sent: Thursday, February 01, 2007 3:25 PM To: python-list@python.org Subject: Re: division by 7 efficiently ??? The correct answer as told to me by a person is (N3) + ((N-7*(N3))3) The above term always gives division

RE: division by 7 efficiently ???

2007-02-01 Thread skip
Michael I think it is off by 1 in small numbers, off by a little more Michael with large numbers: ... Sorta makes you think using the DIV instruction in the CPU is the way to go. ;-) Skip -- http://mail.python.org/mailman/listinfo/python-list

Re: division by 7 efficiently ???

2007-02-01 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], Krypto wrote: The correct answer as told to me by a person is (N3) + ((N-7*(N3))3) How could it be correct if it uses `-`? You ruled out `-` and `/` in your first post. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list

division by 7 efficiently ???

2007-01-31 Thread krypto . wizard
How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer. -- http://mail.python.org/mailman/listinfo/python-list

Re: division by 7 efficiently ???

2007-01-31 Thread Paul Rubin
[EMAIL PROTECTED] writes: How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer. Erm, sounds like a homework problem... suggestion: think of how many input bits you have,

Re: division by 7 efficiently ???

2007-01-31 Thread Ben Finney
[EMAIL PROTECTED] writes: How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer. I think you are asking for help on a homework assignment in violation of collusion and

Re: division by 7 efficiently ???

2007-01-31 Thread Steven D'Aprano
On Wed, 31 Jan 2007 18:42:54 -0800, krypto.wizard wrote: How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer. I'd be surprised if there was a trick for dividing by

Re: division by 7 efficiently ???

2007-01-31 Thread krypto . wizard
Its not an homework. I appeared for EA sports interview last month. I was asked this question and I got it wrong. I have already fidlled around with the answer but I don't know the correct reasoning behind it. Thanks, On Jan 31, 10:01 pm, Ben Finney [EMAIL PROTECTED] wrote: [EMAIL PROTECTED]

Re: division by 7 efficiently ???

2007-01-31 Thread John Nagle
[EMAIL PROTECTED] wrote: Its not an homework. I appeared for EA sports interview last month. I was asked this question and I got it wrong. I have already fidlled around with the answer but I don't know the correct reasoning behind it. The answer to that question is that the fastest way to

Re: division by 7 efficiently ???

2007-01-31 Thread Paddy
On Feb 1, 2:42 am, [EMAIL PROTECTED] wrote: How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer. int.__div__(14,2) 7 Not a minus or division operator in sight ;-) -

Re: division by 7 efficiently ???

2007-01-31 Thread Ben Finney
Paddy [EMAIL PROTECTED] writes: On Feb 1, 2:42 am, [EMAIL PROTECTED] wrote: How to divide a number by 7 efficiently without using - or / operator. int.__div__(14,2) 7 Not a minus or division operator in sight ;-) I salute you, sir. That's the perfect answer to the boneheaded

Re: division by 7 efficiently ???

2007-01-31 Thread Paul McGuire
On Jan 31, 11:36 pm, Paddy [EMAIL PROTECTED] wrote: On Feb 1, 2:42 am, [EMAIL PROTECTED] wrote: How to divide a number by 7 efficiently without using - or / operator. We can use the bit operators. I was thinking about bit shift operator but I don't know the correct answer.

Re: division by 7 efficiently ???

2007-01-31 Thread John Machin
On Feb 1, 2:36 pm, John Nagle [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: Its not an homework. I appeared for EA sports interview last month. I was asked this question and I got it wrong. I have already fidlled around with the answer but I don't know the correct reasoning behind it.