【问题标题】:Bits not being set for a number golang未为数字 golang 设置位
【发布时间】:2015-06-11 09:22:30
【问题描述】:

我正在尝试用 golang 解决project euler problem 3

问题如下:

13195的质因数是5、7、13和29。 600851475143 的最大质因数是多少?

我正在尝试如下解决:

package main

import (
    "fmt"
)

func primeset(n uint64) uint64 {
    primes := uint64(0)
    for p:= uint64(2);p <= n;p++ {
        if((primes & (1 << p)) == 0){
            fmt.Println("Current prime",p)
            for j:=uint64(2)*p;j <=n;j=j+p {
                fmt.Println("Current num:",j)
                primes |= (1 << j)
                fmt.Println("Bitset value is:",primes)
            }
        }
    }
    return primes
}

func main() {
    n := uint64(100)
    primes := primeset(n)
    fmt.Println("Primes is",primes)
    j := n
    for j >= 2 {
        s := primes & (1 << uint64(j))
        if((s == 0) && ((n % j) == 0)){
            fmt.Println("Largest factor",j)
            return
        } else {
            j--
        }
    }

}

在函数“primeset”中,我从一个名为“primes”的无符号整数开始,初始值为 0,然后左移一个数字(这是一个复合数)并将“primes”的那个位设置为 1 .

我的想法是我只需检查“素数”的第 4 位以查看它是否已设置。如果该位已设置,则它不是素数。

对于小数字,代码似乎可以工作,但是当我开始测试它以测试 100 等数字时,突然间事情变得相当奇怪。

我注意到在尝试将其设置为第 62 位之后的位移位不起作用。下面的痕迹可以说明这种情况:

Current num: 48
Bitset value is: 375299968947536
Current num: 50
Bitset value is: 1501199875790160
Current num: 52
Bitset value is: 6004799503160656
Current num: 54
Bitset value is: 24019198012642640
Current num: 56
Bitset value is: 96076792050570576
Current num: 58
Bitset value is: 384307168202282320
Current num: 60
Bitset value is: 1537228672809129296
Current num: 62
Bitset value is: 6148914691236517200
Current num: 64
Bitset value is: 6148914691236517200
Current num: 66
Bitset value is: 6148914691236517200
Current num: 68
Bitset value is: 6148914691236517200
Current num: 70
Bitset value is: 6148914691236517200
Current num: 72
Bitset value is: 6148914691236517200
Current num: 74
Bitset value is: 6148914691236517200
Current num: 76
Bitset value is: 6148914691236517200
Current num: 78
Bitset value is: 6148914691236517200
Current num: 80
Bitset value is: 6148914691236517200
Current num: 82
Bitset value is: 6148914691236517200
Current num: 84
Bitset value is: 6148914691236517200
Current num: 86
Bitset value is: 6148914691236517200

有人能指出我执行位操作的方式有什么问题吗?

谢谢

【问题讨论】:

    标签: go bit-manipulation primes


    【解决方案1】:

    The Go Programming Language Specification

    Arithmetic operators

    <<   left shift             integer << unsigned integer
    >>   right shift            integer >> unsigned integer
    

    移位运算符将左操作数移位移位计数 由右操作数指定。他们实现算术移位,如果 左操作数是有符号整数,如果它是 无符号整数。班次计数没有上限。转移 表现得好像左操作数被移位 n 次 1 以进行移位 n个。

    您正在移动 64 位末尾的位:(1&lt;&lt;p) 其中p &gt; 63。例如,

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        primes := ^uint64(0)
        fmt.Println(primes)
        for _, p := range []uint64{0, 1, 2, 62, 63, 64, 65, 99, 100} {
            fmt.Println(p, "\t", primes&(1<<p))
        }
    }
    

    输出:

    18446744073709551615
    0    1
    1    2
    2    4
    62   4611686018427387904
    63   9223372036854775808
    64   0
    65   0
    99   0
    100  0
    

    【讨论】:

      猜你喜欢
      • 2018-12-25
      • 2022-07-21
      • 2015-07-25
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      相关资源
      最近更新 更多