有效地划分7? [英] division by 7 efficiently ???

查看:76
本文介绍了有效地划分7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用 - 或/运算符的情况下有效地将数字除以7.

我们可以使用位运算符。我在想点位移操作员

但是我不知道正确的答案。

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.

推荐答案

kr***********@gmail.com 写道:

如何在不使用 - 或/运算符的情况下有效地将数字除以7.

我们可以使用位运算符。我在想点位移操作员

但是我不知道正确的答案。
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, then check the accuracy of the obvious
approximations until you reach one that''s precise enough.


kr *********** @ gmail.com 写道:
kr***********@gmail.com writes:

如何在不使用 - 或/

运算符的情况下有效地将数字除以7。我们可以使用位运算符。我在考虑一下

班次操作员,但我不知道正确的答案。
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.



我认为您正在寻求家庭作业的帮助违反

你的教育机构的勾结和抄袭规则,以及/>
我要求我的十个zorkmids。


请使用您在

学习课程中可用的资源;这几乎肯定不包括让人们在讨论论坛上为你回答作业。


-

\" ;我喜欢去艺术博物馆,并命名无题画作。 |

` \''男孩有桶''。 ''小猫着火''。 - Steven Wright |

_o__)|

Ben Finney

I think you are asking for help on a homework assignment in violation
of collusion and plagiarism rules of your educational institution, and
I claim my ten zorkmids.

Please use the resources made available to you in your course of
study; this almost certainly does not include having people on
discussion forums answer the homework for you.

--
\ "I like to go to art museums and name the untitled paintings. |
`\ ''Boy With Pail''. ''Kitten On Fire''." -- Steven Wright |
_o__) |
Ben Finney


1月31日星期三2007 18:42:54 -0800,krypto.wizard写道:
On Wed, 31 Jan 2007 18:42:54 -0800, krypto.wizard wrote:

如何在不使用 - 或/运算符的情况下有效地将数字除以7。

我们可以使用位运算符。我在想点位移操作员

但是我不知道正确的答案。
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.



如果有一个技巧可以有效地除以7,我会感到惊讶...

它是其中之一困难"案例。请参阅:

http:// www。 bitwisemag.com/copy/wilf/wilf5.html


进行讨论。


但即使有一些俏皮使用位运算符有效地除以
七的算法,我不认为它会在
Python中高效工作。使用对象的开销可能会超过你使用魔术技巧可能获得的任何节省,所以你应该使用除法。


这就像使用交换整数的XOR技巧:除了作为一个技巧之外,在Python中使用它只是没有

点,因为它比

只是交换它们要慢:

I''d be surprised if there was a trick for dividing by seven efficiently...
it is one of the "difficult" cases. See:

http://www.bitwisemag.com/copy/wilf/wilf5.html

for a discussion.

But even if there is some nifty algorithm for efficiently dividing by
seven using bit operators, I don''t expect it will work efficiently in
Python. The overhead of using objects will probably outweigh any saving
you might get by using magic tricks, so you should just use division.

It is like using the XOR trick for swapping integers: there''s just no
point in doing it in Python except as a trick, because it is slower than
just swapping them:


>> timeit.Timer(" x = x ^ y; y = x ^ y; x = x ^ y"," x,y = 1,2")。repeat()
>>timeit.Timer("x = x^y; y = x^y; x= x^y", "x, y = 1, 2").repeat()



[0.88827300071716309 ,0.85192012786865234,0.81278204917907715]

[0.88827300071716309, 0.85192012786865234, 0.81278204917907715]


>> timeit.Timer(" x,y = y,x"," x,y = 1,2")。repeat()
>>timeit.Timer("x, y = y, x", "x, y = 1, 2").repeat()



[0.71292495727539062,0.655545105934143066, 0.68413901329040527]

-

Steven D''Aprano

[0.71292495727539062, 0.65545105934143066, 0.68413901329040527]
--
Steven D''Aprano


这篇关于有效地划分7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆