1

This is in regards to project Euler problem #1. When I run this code line by line in irb, I get the expected answer, but when I run it from a .rb file, it throws error "Array can't be coerced into Fixnum (TypeError)"

Code:

# defines the list of integers from 'n' downto 0
def Zi (a, b) 
   (a.downto b).lazy
end

# the equation numberOfOccurances(smallestMultiple + largestMultiple)/2 gives the sum of all the multiples for a given range
# so, below, multipleCounts[i](multiples[i] + lastMultiples[i])/2 => multipleCounts[i](lastAndFirstMultipleSums[i])/2
# gives the arbitrary sum of multiples of a give number in multiples for the specified range.
def find_sum_for_each_multiple(from, to, *multiples)
   myReverseArr = Zi to, from
   lastMultiples = multiples.map { |m| myReverseArr.find { |i| i % m == 0 } }
   multipleCounts = lastMultiples.zip(multiples).map { |ms| ms.reduce(:/) }
   lastAndFirstMultipleSums = lastMultiples.zip(multiples).map { |ms| ms.reduce(:+) }
   sumsOfEachMultiple = lastAndFirstMultipleSums.zip(multipleCounts).map { |xs| xs.reduce(:*) }.map { |x| x/2 }
end

def find_sum_of_multiples(from, to, *multiples)
   sumsOfEachMultiple = find_sum_for_each_multiple(from, to, multiples)

   commonMultiples = []
   (0..(multiples.length - 1)).each do |i|
      ((i+1)..(multiples.length - 1)).each do |j|
         commonMultiples << (multiples[i] * multiples[j])
      end
   end

   sumsOfCommonMultiples = find_sum_for_each_multiple(from, to, commonMultiples)

   totalSum = (sumsOfEachMultiple.inject { |sum, x| sum + x }) - (sumsOfCommonMultiples.inject { |sum, x| sum + x })
end

puts find_sum_of_multiples(0, 999, 3, 5)

Error message:

C:\Users\[User]>ruby euler1.rb
euler1.rb:11:in `%': Array can't be coerced into Fixnum (TypeError)
        from euler1.rb:11:in `block (2 levels) in find_sum_for_each_multiple'
        from euler1.rb:11:in `downto'
        from euler1.rb:11:in `each'
        from euler1.rb:11:in `each'
        from euler1.rb:11:in `find'
        from euler1.rb:11:in `block in find_sum_for_each_multiple'
        from euler1.rb:11:in `map'
        from euler1.rb:11:in `find_sum_for_each_multiple'
        from euler1.rb:18:in `find_sum_of_multiples'
        from euler1.rb:32:in `<main>'

I'm running on a core i7 Windows 7 64bit machine with Ruby version 2.0.0p0 (2013-02-24) [x64-mingw32]

1
  • whoever down-voted me, mind explaining why I was down-voted? This seems like a legitimate question to me... If I can execute the same code line by line in the interpreter and get no errors, but I get an error when running the code from a script, this would imply at least to me that my logic is correct, but there is something specific about the language implementation that I am missing. I did Google around a little, but I didn't find much that I thought pertained to this error. Could it be a defect in the interpreter? Commented Aug 12, 2013 at 6:57

1 Answer 1

1

commonMultiples is already an Array, and in find_sum_for_each_multiple, it gets converted to an Array of Arrays

Change this line

sumsOfCommonMultiples = find_sum_for_each_multiple(from, to, commonMultiples)

to

sumsOfCommonMultiples = find_sum_for_each_multiple(from, to, *commonMultiples)
Sign up to request clarification or add additional context in comments.

3 Comments

Aaah! So it was just a bit of Ruby syntax I was missing. Thanks! This worked for me!
I also want to add that I know this isn't a full solution yet - if you try this method with 3 or more multiples it will break because the sums of common multiples calculation doesn't yet find all of the common multiple duplications. To do so, I believe I would have to edit the code to calculate not only the sums of the common multiples of all pairs (2s), but also of triplets (3s), etc. on up to n where n is the number of multiples. Still sort of thinking this one out.
I didnt try to understand the logic behind this code. Anyways good luck with the problem. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.