2 min read

Project Euler Problem 52 solution in Ruby

Hi, you’d probably be wondering why I started all the way at 52, like what happened to problems 1 to 51, and what the heck is Project…

Hi, you’d probably be wondering why I started all the way at 52, like what happened to problems 1 to 51, and what the heck is Project Euler. To start with, Project Euler is a site where you write code for a variety of mathematical problems. You can use any language you want. Each problem usually just requires a single answer, so you’ll write your code and run it and submit to see if you solved it correctly. And to why I started at 52, this is the first time I’m doing this, so I started with a problem I just solved.

Problem — It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order. Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

Solution

I’ll assume you have Ruby installed. If you don’t, you can get it from here. To run, copy the code below and save to a new file with a `.rb` extension. Navigate to the directory containing it on your command line and run `ruby filename.rb`

def permuted_multiples
 puts (1..1000000).select{|num| (2..6).all? { |val|
   (num * val).to_s.chars.sort == num.to_s.chars.sort
  }}
end

permuted_multiples

Breakdown

This should help newbies or developers not familiar with Ruby better understand the solution

(1..1000000)

This creates an array of the numbers from 1 to a million

.select{|num| ...}

This returns numbers in that range matching a particular property specified within the block

(2..6).all? { |val| ... }

This is like an iterative block, that passes the numbers 2 to 6 to the block. An all block will perform comparisons with each member in the collection passed to it, , and only returns true if comparisons using all the members of that collection evaluate to true. Say 3 was passed to the block, and the comparison made using it evaluated to false or nil, it returns false regardless of whether the remaining were true. Remember we are looking for a solution were x, 2x, 3x, 4x, 5x, and 6x all have the same digits, so we can’t have any of them failing.

(num * val).to_s.chars.sort == num.to_s.chars.sort

This does the actual comparison with the numbers passed into the all block. Val would be the number passed into the all block and num would be the number that we are checking to see if it satisfies the property in the question. We’ll multiply both at each point in the all block, convert it to a string (to_s), then create a character array from the string(chars), and sort it. Once we’re done, we compare it to the original number after the same operations have been performed on it.

puts

In some other languages, I’d have to assign the result to a variable, then print it out on a separate line. But in Ruby, I can prepend puts (which is used for printing) to the beginning of the operation, and it prints out the result once it’s done. Helps save memory that would have been consumed by a variable, had we created one to hold the result.

Hope you enjoyed it, more to come later. Here’s a link to my repo where you can find solutions to other problems I’ve solved — https://github.com/robocopkaka/project_euler