如何检查是否给定的数字是最快的方式整除的15? [英] How to check if given number is divisible of 15 in fastest way?

查看:171
本文介绍了如何检查是否给定的数字是最快的方式整除的15?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

司在处理器要花很多时间,所以我想问问如何检查在最快的方式,如果数整除一些其他的一些,在我来说,我需要检查,如果数字是整除15。

Division in processor takes much time, so I want to ask how to check in fastest way if number is divisible of some other number, in my case I need to check if number is divisible by 15.

此外,我一直在寻找通过网页,发现的乐趣的方法来检查,如果数整除的数的一些,但是我正在寻找快速的选择。

Also I've been looking through web and found fun ways to check if number is divisible of some number, but I'm looking for fast option.

注意:的分工需要我在寻找的答案多少时间没有 /

NOTE: as division takes much time I'm looking for answer without / and %.

推荐答案

乘花费较少的时间再划分,这样你就可以试试这个:

Multiplication takes less time then division, so you can try this:

inline bool divisible15(unsigned int x)
{
    //286331153 = (2^32 - 1) / 15
    //4008636143 = (2^32) - 286331153
    return x * 4008636143u <= 286331153u;
}

这样的作品,因为 2 ^ 32-1 (最多32位值)整除的15,但是如果你拿,例如7,它会是什么样子工作,但不会在所有情况下工作。

This way works because 2^32-1 (max 32-bit value) is divisible of 15, however if you take, for example 7, it would look like working, but wouldn't work in all cases.

编辑:查看,证明该解决方案(在一些编译器)更快然后模块。

See this, it proves that this solution (on some compilers) is faster then module.

这篇关于如何检查是否给定的数字是最快的方式整除的15?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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