Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Bignum太大,无法转换为';长';(范围错误)_Ruby_Bignum - Fatal编程技术网

Ruby Bignum太大,无法转换为';长';(范围错误)

Ruby Bignum太大,无法转换为';长';(范围错误),ruby,bignum,Ruby,Bignum,试着自学ruby——我正在用ruby处理Euler项目的问题14 n = 1000000 array = Array.new(n,0) #array[x] will store the number of steps to get to one if a solution has been found and 0 otherwise. x will equal the starting number. array[0] will be nonsensical for these purposes

试着自学ruby——我正在用ruby处理Euler项目的问题14

n = 1000000
array = Array.new(n,0)
#array[x] will store the number of steps to get to one if a solution has been found and 0 otherwise. x will equal the starting number. array[0] will be nonsensical for these purposes
i = n-1#We will start at array[n-1] and work down to 1
while i > 1
    if array[i] == 0
        numstep = 0 #numstep will hold the number of loops that j makes until it gets to 1 or a number that has already been solved
        j = i
        while j > 1 && (array[j] == 0 || array[j] == nil)
            case j%2
            when 1 # j is odd
                j = 3*j + 1
            when 0 # j is even
                j = j/2
            end
            numstep += 1
        end
        stop = array[j] #if j has been solved, array[j] is the number of steps to j = 1. If j = 1, array[j] = 0
        j = i
        counter = 0
        while j > 1 && (array[j] == 0 || array[j] == nil)
            if j < n
                array[j] = numstep + stop - counter #numstep + stop should equal the solution to the ith number, to get the jth number we subtract counter
            end

            case j%2
            when 1 #j is odd
                j = 3*j+1
            when 0 #j is even
                j = j/2
            end
            counter += 1
        end
    end
    i = i-1
end

puts("The longest Collatz sequence starting below #{n} starts at #{array.each_with_index.max[1]} and is #{array.max} numbers long")
第27行(即while语句:

while j > 1 && (array[j] == 0 || array[j] == nil)

如何使其不抛出错误?检查数组是否为零可以节省代码效率,因为它允许您不重新计算已经完成的某些操作,但如果我删除and语句,它将运行并给出正确答案。我很确定问题在于数组的索引不能是bignum,但可能存在一个声明数组的方法?我不太关心答案本身;实际上我已经用C#解决了这个问题-只是尝试学习ruby,所以我想知道为什么我的代码会这样做(如果我对原因有错误的话)以及如何修复。

对于在可接受的时间内产生输出的任何输入,上面的代码对我来说都运行得很好。我相信这是因为您可能会遇到32位arch之类的问题。无论如何,所述问题的解决方案很简单(除非您可能会耗尽内存,这是另一个可能的故障)

数组索引是有限的,从你得到的错误来看如下。酷,让我们用散列代替

n = 1000000
array = Hash.new(0)
#array[x] will store the number of steps to get to one if a solution has been found and 0 otherwise. x will equal the starting number. arr
i = n-1#We will start at array[n-1] and work down to 1
while i > 1 
  if array[i].zero?
        numstep = 0 #numstep will hold the number of loops that j makes until it gets to 1 or a number that has already been solved
        j = i 
        while j > 1 && array[j].zero?
            case j%2 
            when 1 # j is odd
                j = 3*j + 1 
            when 0 # j is even
                j = j/2 
            end
            numstep += 1
        end
        stop = array[j] #if j has been solved, array[j] is the number of steps to j = 1. If j = 1, array[j] = 0
        j = i 
        counter = 0 
        while j > 1 && array[j].zero?
            if j < n 
                array[j] = numstep + stop - counter #numstep + stop should equal the solution to the ith number, to get the jth number we 
            end

            case j%2 
            when 1 #j is odd
                j = 3*j+1
            when 0 #j is even
                j = j/2 
            end
            counter += 1
        end
    end 
    i = i-1 
end


puts("Longest Collatz below #{n} @#{array.sort_by(&:first).map(&:last).each_with_index.max[1]} is #{arr
n=1000000
数组=散列。新建(0)
#如果找到解决方案,数组[x]将存储到达一的步骤数,否则为0。x将等于起始数。arr
i=n-1#我们将从数组[n-1]开始,一直到1
当i>1时
如果数组[i].0?
numstep=0#numstep将保留j生成的循环数,直到它变为1或一个已求解的数字
j=i
当j>1&&数组[j].为零时?
案例j%2
当1#j是奇数时
j=3*j+1
当0#j为偶数时
j=j/2
结束
numstep+=1
结束
stop=array[j]#如果已经解出j,则array[j]是j=1的步数。如果j=1,则array[j]=0
j=i
计数器=0
当j>1&&数组[j].为零时?
如果j

请注意,由于我使用了带有初始值设定项的哈希,
array[I]
不能变成
nil
,这就是为什么只对零值进行检查。

2997502
不是太大,无法转换为
long
,它甚至不是一个特别大的数组。不知何故,您创建了一个更大的数字。您的程序运行良好,并打印正确的结果。您使用的是哪个版本的rubying?谢谢,我不知道哈希。使用它对我来说很好。注意,你的puts语句工作不太正确-它返回的是第二高的数字,不是最高的,但我不知道为什么。读了一些关于哈希的书,并将其替换为:puts(“在#{n}下面最长的Collatz序列是#{array.max_by{k,v | v}[1])数字很长,从#{array.max_by{k,v | v}[0]}开始。”)我没有太注意输出,我只是想展示一个解决方案。
n = 1000000
array = Hash.new(0)
#array[x] will store the number of steps to get to one if a solution has been found and 0 otherwise. x will equal the starting number. arr
i = n-1#We will start at array[n-1] and work down to 1
while i > 1 
  if array[i].zero?
        numstep = 0 #numstep will hold the number of loops that j makes until it gets to 1 or a number that has already been solved
        j = i 
        while j > 1 && array[j].zero?
            case j%2 
            when 1 # j is odd
                j = 3*j + 1 
            when 0 # j is even
                j = j/2 
            end
            numstep += 1
        end
        stop = array[j] #if j has been solved, array[j] is the number of steps to j = 1. If j = 1, array[j] = 0
        j = i 
        counter = 0 
        while j > 1 && array[j].zero?
            if j < n 
                array[j] = numstep + stop - counter #numstep + stop should equal the solution to the ith number, to get the jth number we 
            end

            case j%2 
            when 1 #j is odd
                j = 3*j+1
            when 0 #j is even
                j = j/2 
            end
            counter += 1
        end
    end 
    i = i-1 
end


puts("Longest Collatz below #{n} @#{array.sort_by(&:first).map(&:last).each_with_index.max[1]} is #{arr