Caleb Imler
Caleb Imler

Reputation: 11

Euler #3 in ruby. What am I not understanding about ruby?

Project Euler 3:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

I don't want to just google the solution for this problem, because I'd like to come to understand this language.. Basically, with what I have right now, if I run it, my terminal stops responding. No error messages or anything. I'm guessing that's an infinite loop?

I'm trying to find the prime factors under 1000. I've looked up the sieve of eratosthenes and have no idea how I would write up one of those.. Thanks in advance for any and all help.

$factors = []
$i = 1

def factor(n)
    $i += 1
    while $i < n do
        $factors << $i if ($i < 1000 && n % $i == 0)
    end
end

factor(600851475143)

puts $factors

Upvotes: 1

Views: 146

Answers (3)

mrodrigues
mrodrigues

Reputation: 1082

Well, since the $i += 1 is outside the loop, it won't ever finish.

I'd advise you not to use global variables, though it may not be your focus right now.

Upvotes: 2

Jared Archey
Jared Archey

Reputation: 309

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

Upvotes: 0

Amadan
Amadan

Reputation: 198408

You have a loop that will run until the relationship between $i and n changes in a certain defined way. This will be a bit difficult, given that neither $i nor n budges from their initial value inside the loop.

Since you are rather far from the solution, I am not sure how to tell you how to write it without actually writing it for you. The best I can do is suggest to look at the pseudocode and try to transcribe it word-for-word into Ruby.

Another thing I wanted to note is your reliance on global variables. They are almost never used in Ruby, as they indicate structural issues with the code. They are almost always a bad idea. While not using classes, use only normal variables (i, factors...) instead of global ones ($i, $factors...).

Upvotes: 2

Related Questions