0
숫자가 2 진수가 될 수있는 방법을 찾는 데 필요한 CodeEval 문제를 수행하고 있습니다. 내가 명령 줄에서 프로그램을 실행하면메모리를 할당하지 못함 수신 오류
그것이 내가에 프로그램을 전송할 때 그러나, 나는이 오류가 계속 매우 빠르게 올바른 솔루션을 출력 : 여기에 문제에 대한 링크입니다 CodeEval 읽습니다.
"치명적 : 메모리 할당 실패".
저는 프로그래밍에 익숙하지 않아 이것이 발생하는 이유를 모릅니다. 누군가 제게 이것을 설명해 주시겠습니까?
def double_square(x)
#Create array for storing double squares
arr = []
#Make x an integer
x = x.to_i
#Solve for case 0
if x == 0 then arr << 0 end
sqrt_x = Math.sqrt(x)
sqrt_x_as_int = Math.sqrt(x).to_i
#Check if x is a perfect square, if it is add it to array with '0'
if sqrt_x/sqrt_x_as_int == 1.0
arr << [0,x]
end
#Find squares of all numbers less than the square root of x
squares = (1..sqrt_x_as_int).map {|num| num**2}
#Create array containing the combinations of squares & if array contains x, delete it
combos = squares.combination(2).to_a.delete_if {|combo| combo.any? {|num| num == x}}
#Find the sum of each array and select the arrays whose sums are equal to x
sums = combos.map do |combo|
sum = combo.inject(:+)
if sum == x
arr << combo
end
end
#Return the amount of double squares for n
puts arr.count
end
lines = File.readlines(ARGV[0]).map {|line| line.strip}
lines[0].to_i.times {|i| double_square(lines[i+1])}