화면상의 제 방법 중 puts
또는 print
을 사용할 수없는 이유를 알아내는 데 필요한 피드백이 필요합니다. 이것은 제가 1001 번째 소수를 찾는 문제를 해결하기 위해 작성한 간단한 스크립트입니다. 감사합니다Ruby - 소수 번호 계산기
def primes
# iterates through numbers until it has the 1001th prime number and returns it.
# I chose to create the num_primes variable instead of counting the number of
# elements in in_prime_array every iteration
# based upon a guess that it would be faster to check.
is_prime_array = []
num_primes = 0
i = 2
loop do
is_prime_array << i && num_primes += 1 if is_prime?(i) == true
i += 1
break if num_primes == 1001
end
is_prime_array[1001]
end
def is_prime? (num)
# Checks to see if the individual number given is a prime number or not.
i = 2
loop do
if i == num
return true
elsif num % i == 0
return false
else
i += 1
end
end
end
어떤 도움을 주셔서 감사합니다!
편집 나는 당신의 충고를 따랐다하고이 코드 파이스 동전 시도 :
def is_prime? (num)
# Checks to see if the individual number given is a prime number or not.
i = 2
loop do
if i == num
return true
elsif num % i == 0
return false
else
i += 1
end
end
end
i = 0
count = 0
loop do
count += 1 if is_prime?(x)
puts "#{i}" if count == 1001
break
end
그것은 여전히 아무 것도 반환하지 않습니다를. Hummm
?'(가)'loop',로 변경 내부'나는> num''TRUE ''ELSIF를 반환 * 경우 ... '. –
들여 쓰기가 엄격 해지면 삶이 더 나아질 것입니다. 'i = 2'와'is_prime_array = []'는 2 칸 이내 여야합니다. –