From what I can tell, I believe the issue with your terminal not responding is because you are looping to such a high number from 2. When you call factor(600851475143), your n is 600851475143 so you are basically running code like
counter = 0
counter += 1 while counter < 600851475143
If you run the above code it produces the same error you described
You can also notice the time it takes for your computer to finish the while loop above gets longer as you increase the number as you can find out by uncommenting one loop at a time below
counter = 0
counter += 1 while counter < 100851475143 #Still gives blank screen
#counter += 1 while counter < 851475143 #Much smaller but still produces the error
#counter += 1 while counter < 51475143 #No error, but does not finish immediately
#counter += 1 while counter < 1475143 #Finished almost immediately
For the problem you described, I would recommend writing a helper function to calculate the first multiple of the number
def first_multiple(number)
return number if number <= 2
multiple = 2
multiple += 1 while number%multiple != 0
multiple
end
This function also tells if the number is prime if number passed in equals the output. Using the above function you can create an array with the number divided by its first_multiple and the first multiple, so 10 would be [2, 5]. Then you can loop through this array and create a new array by dividing each number by its first multiple if the number does not equal the multiple. Keep doing this step until all elements of the array equals its first multiple
It could also be helpful to look into some of the methods for the array class like Array.any? which returns true if any element of the array satisfies the block