Why am I getting all these nils - Euler 3 - ruby

I came up with this solution for Project Euler 3 although I got the answer I also get so many nils before it I can't figure out why? is there any way I can only get the answer.
Anyway here is my code
def factor(number)
max = []
(2...number).each do |x|
if number % x == 0
number = number/x
max << number
s = max[-2]
elsif number == 1
return[]
end
puts s
end
end
puts factor(600851475143)

max is an empty array. s = max[-2] is s =[][-2] for a long time. [][-2] returns nil.

Related

Trying to solve for the largest prime factor without recursion - Ruby

I came across a website called Project Euler and everything was going well until I hit the 3rd problem - The Largest Prime Factor. I don't want to use recursion to solve it. I saw solutions online where they use Math.sqrt and I don't want to use that either. Stubborn, I know.
I'd like to solve it with just loops and if statements. I assumed the input is an odd number. Here is my code. The output keeps coming out as [3] if num = 99 and I can't figure out why. I tried putting a puts statement everywhere to see what was being outputted at each step. One issue I realized was that that the array#p was not resetting after each loop. I tried array.clear but that wasn't much help. Could someone point me in the right direction? Is there some fundamental aspect about arrays, loops, and if-statements that I'm not getting?
def prime(num)
arr = []
p = []
not_p = []
# first I find all the numbers that num is divisible by
for i in (2..num/2)
if num % i == 0
arr << i
end
end # this should output [3, 9, 11, 33]
arr.each do |x| # I loop through each element in the above array
for i in (2..(x/2)) # I divide each element - x - by 2 because it cannot be divisble by anything greater than its half
if x % i == 0 # if x is divisble by i
not_p << i # I push the i into array#not_p
end # keep looping until i reaches x/2
end
if not_p.length == 0 # if there are no values in array#not_p, then I know x is a prime factor
p << x # so I push x into array#p
end
end
return p[-1] # returns the last element of the array, which is the largest
end
puts prime(99)
I'm not going to give you the full answer, as that would defeat the object of the practice with Project Euler.
However, you're almost on the right track with sorting out your problem. You don't want to look at the array p not being emptied, that should be collecting your primes. You do want to look at not_p though, since that is the array of divisors of each of your factors.
I hope this helps. Let me know if I can help any more.
Ah ok! Thanks for the suggestion philnash! In fact, I knew about that problem and tried to clear the array with Array.clear but that did not work. Instead, I just moved not_p = [] below the iteration arr.each do |x| and it worked! It makes sense because the not_p resets to [] when it moves on to the next element. Thanks so much for your help and for not providing the answer first! Here is my final, working solution =D
def prime(num)
arr = []
p = []
for i in (2..num / 2)
if num % i == 0
arr << i
end
end # this should output [3, 9, 11, 33]
arr.each do |x|
not_p = []
for i in (2..(x / 2))
if x % i == 0
not_p << i
end
end
if not_p.length == 0
p << x
end
end
return p[-1]
end
puts prime(99) # => 29

Error accessing an array's value by it's index?

Trying to sum up all the numbers in an array. Example 10 + 20 + 30 should be 60.
def sum *arr
i=0
total=0
while i <= arr.count
total += arr[i]
i+=1
end
total
end
puts sum(10,20,30)
Why am I getting this error. This code looks like it should work to me. What am I doing wrong? Why wont it let me access the array value by it's index?
p8.rb:23:in `+': nil can't be coerced into Fixnum (TypeError)
from p8.rb:23:in `sum'
from p8.rb:29:in `<main>'
Change
while i <= arr.count
to
while i < arr.count
arr[arr.count] is always out of bounds.
Fyi a shorter way to write sum is:
def sum *arr
arr.inject(:+)
end
Matt's answer gives you the canonical Ruby way to sum, using inject. But if you're not ready to learn inject, at least save yourself the trouble of manually keeping track of array indexes (where your actual problem lies!) by using #each to iterate through the array like so:
def sum *arr
total = 0
arr.each do |x|
total += x
end
total
end
puts sum(10,20,30) # => 60
Matt's answer is both slick and correct, but you hit the error because ruby zero indexes arrays. So if you changed the count condition
while i <= arr.count - 1
your error would go away

Receiving Failed to Allocate Memory Error

I am doing a CodeEval problem asking to find how many ways a number can become a double square. Here's the link to the problem:
https://www.codeeval.com/open_challenges/33
When I run the program from the command line it outputs the correct solution very quickly, however, I keep getting an error when I submit the program to CodeEval that reads:
"FATAL: Failed To Allocate Memory".
I'm new to programming and not sure why this is occurring, can someone please explain this to me.
Here is my code:
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])}
I would guess it's a problem with CodeEval's server or sandbox environment.

Random number generator issues in Ruby

My intention here is just to fill up an array with numbers in order from 1, to a random number between 1 and 1000. However, after repeatedly running this code (about 50 times), the highest number I have gotten is 120, and only twice has it been over 100. The majority of my arrays were anywhere between 0 and 60. This behavior appears off to me. Am I doing something wrong?
my_array = []
i = 0
while i <= rand(1000)
my_array << i
i += 1
end
puts my_array.count
puts my_array
Your function is broken, because you're checking versus the random number. Do this:
(0..1000).collect{ rand(1000) }
This will return an array of one thousand random numbers.
Or, closer to your code:
my_array = []
i = 0
while i <= 1000
my_array << rand(1000)
i += 1
end
As per comment, what you want is:
(1..rand(1000))
(1..rand(1000)).to_a
The first results in a range, which is "easier to carry around", the second results in the populated array.
(Edit) Note:
(1..10) is inclusive - (1..10).to_a == [1,2,3,4,5,6,7,8,9,10]
(1...10) is partially exclusive - (1...10).to_a == [1,2,3,4,5,6,7,8,9] - it does not include the end of the array, but still includes the beginning.
It sounds like you want:
(1...rand(1000)).to_a
Additionally, I have amended my code to reflect what I was trying to accomplish initially. My problem was that every time I looped through my code I generated a new random number. Because of this, as 'i' incremented toward 1000 it became more and more likely that a random number would be generated that was lower than 'i'. My fix, while not as elegant as the solution above that I accepted, was to store the random number in a variable, BEFORE attempting to use it in a loop. Thanks again. Here is the amended code:
my_array = []
i = 0
g = rand(1000)
while i <= g
my_array << i
i += 1
end
puts my_array.count
puts my_array

optimize this ruby code

So this code will count the total number of pairs of numbers whose difference is K. it is naive method and I need to optimize it. suggestions?
test = $stdin.readlines
input = test[0].split(" ")
numbers = test[1].split(" ")
N = input[0]
K = input[1]
count = 0
for i in numbers
current = i.to_i
numbers.shift
for j in numbers
difference = (j.to_i - current).abs
if (difference == K)
count += 1
end
end
end
puts count
Would have been nice for you to give some examples of input and output, but I think this is correct.
require 'set'
def count_diff(numbers, difference)
set = Set.new numbers
set.inject 0 do |count, num|
set.include?(num+difference) ? count+1 : count
end
end
difference = gets.split[1].to_i
numbers = gets.split.map { |num| num.to_i }
puts count_diff(numbers, difference)
Untested, hopefully actual Ruby code
Documentation for Set: http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/classes/Set.html
require 'set'
numbers_set = Set.new
npairs = 0
numbers.each do |number|
if numbers_set.include?(number + K)
npairs += 1
end
if numbers_set.include?(number - K)
npairs += 1
end
numbers_set.add(number)
end
Someone deleted his post, or his post was deleted... He had the best solution, here it is :
test = $stdin.readlines
input = test[0].split(" ")
numbers = test[1].split(" ")
K = input[1]
count = 0
numbers.combination(2){|couple| couple.inject(:-).abs == K ? count++}
puts count
You don't even need N.
I do not know Ruby so I'll just give you the big idea:
Get the list
Keep a boolean array (call it arr), marking off numbers as true if the number exists in the list
Loop through the list and see if arr[num-K] and/or arr[num+K] is true where num is a number in your list
This uses up quite a bit of memory though so another method is to do the following:
Keep a hash map from an integer n to an integer count
Go through your list, adding num+K and num-K to the hash map, incrementing count accordingly
Go through your list and see if num is in the hash map. If it is, increment your counter by count

Resources