#Collatz conjecture

6 messages · Page 1 of 1 (latest)

fringe hill
#

Hello, I'm trying to solve the collatz conjecture in Julia but unfortunately it when I submit my code it tells me that I have two test failures. When testing x = 0 and x = -15 it should return a DomainError but my code returns an ErrorException. When I run the same code in VScode it does thro the DomainError. Does anyone know my mistake ? Thank you in advance.

function collatz_steps(x)
  if x < 1
    throw(error(DomainError))
  end
  steps = 0
  while x > 1
    if x % 2 == 0
      x /= 2
    else
      x = 3x +1
    end
    steps +=1
  end
  return steps
end
errant kettle
#

should it not be (3 * x) + 1 in your else condition?

median ore
#

error() throws an ErrorException as documented. See throw in the manual for a DomainError example

median ore
errant kettle
#

oh really? wow!