# Project Euler 51: Ruby

Filed under:
|
|
##### ruby

In my attempt to learn Ruby out in the open, here’s my solution for Project Euler Problem 51

I know I started back up with Python this week, but I have three more Ruby solutions in the hopper and I wanted to share. For the record, Project Euler 51 was the second hardest Euler problem for me thus far. Yeah.

As always, any feedback is welcome.

```# Euler 51
# http://projecteuler.net/index.php?section=problems&id=51
# By replacing the 1st digit of *3, it turns out that six
# of the nine possible values: 13, 23, 43, 53, 73, and 83,
# are all prime.
#
# By replacing the 3rd and 4th digits of 56**3 with the
# same digit, this 5-digit number is the first example
# having seven primes among the ten generated numbers,
# yielding the family: 56003, 56113, 56333, 56443,
# 56663, 56773, and 56993. Consequently 56003, being the
# first member of this family, is the smallest prime with
# this property.
#
# Find the smallest prime which, by replacing part of the
# number (not necessarily adjacent digits) with the same
# digit, is part of an eight prime value family.

timer_start = Time.now

require 'mathn'

def eight_prime_family(prime)

0.upto(9) do |repeating_number|
# Assume mask of 3 or more repeating numbers
if prime.count(repeating_number.to_s) >= 3
ctr = 1

(repeating_number + 1).upto(9) do |replacement_number|
family_candidate = prime.gsub(repeating_number.to_s,
replacement_number.to_s)

ctr += 1 if (family_candidate.to_i).prime?
end

return true if ctr >= 8
end
end

false
end

# Wanted to loop through primes using Prime.each
# but it took too long to get to the starting value.
n = 9999
while n += 2
next if !n.prime?
break if eight_prime_family(n.to_s)
end

puts n
puts "Elapsed Time: #{(Time.now - timer_start)*1000} milliseconds"
```

© Johnny Coder or respective owner

• #### Programming languages, positional languages and natural languages

as seen on Programmers - Search for 'Programmers'
Some programming languages are modeled on machine code, like assembly languages. Other languages are modeled on a natural language, the English language. Others are not modeled on either machine code or natural language. Languages such as PROLOG, for example, don't follow either model. I came across… >>> More

• #### Dynamic type languages versus static type languages

as seen on Stack Overflow - Search for 'Stack Overflow'
What are the advantages and limitations of dynamic type languages compared to static type languages? See also: whats with the love of dynamic languages (a far more argumentative thread...) >>> More

• #### Are Mark Up languages considered programming languages?

as seen on Stack Overflow - Search for 'Stack Overflow'
Hi Are all those markup languages programming languages? For example XML or HTML are programming languages? >>> More

• #### Managed Languages vs Compiled Language difference?

as seen on Programmers - Search for 'Programmers'
I get confused when people try to make a distinction between compiled languages and managed languages. From experience, I understand that most consider compiled languages to be C,C++ while managed languages are Java,C# (There are obviously more, but these are just few examples). But what exactly is… >>> More

• #### Advantages of compilers for functional languages over compilers for imperative languages

as seen on Stack Overflow - Search for 'Stack Overflow'
As a follow up to this question What are the advantages of built-in immutability of F# over C#?--am I correct in assuming that the F# compiler can make certain optimizations knowing that it's dealing with largely immutable code? I mean even if a developer writes "Functional C#" the compiler wouldn't… >>> More

### Related posts about Project Euler

• #### VS2010: Warning on add project reference to Silverlight project from .NET project

as seen on Stack Overflow - Search for 'Stack Overflow'
In VS2010, Silverlight 4, .NET 4, I've got a WCF service and a Silverlight app, and Silverlight is accessing the class not with Add Service Reference but by sharing the contract. Naturally, this means I have the contract in a Silverlight class library, and the service has a project reference to that… >>> More

• #### Ant calling child project parent project ivy.xml overriding child project ivy.xml

as seen on Stack Overflow - Search for 'Stack Overflow'
We've got a parent project and a child project java builds using ant and ivy. The child project needs to be able to build independently. When the parent project calls the child project the antfile command - the parent project's ivy.xml overrides the child project's ivy.xml Does anyone know how… >>> More

• #### Project-Based ERP - The Evolution of Project Managemen

as seen on Oracle Blogs - Search for 'Oracle Blogs'
Fred Studer speaks with Ray Wang, Principal Analyst at Forrester Research and Ted Kempf, Senior Director for Oracle's Project Management Solutions about trends in the project management market, where enterprise project management is heading in the next 2 - 3 years and highlights from Ray's new line… >>> More

• #### How can I setup dependencies for Axis2 / Axiom on Maven2

as seen on Stack Overflow - Search for 'Stack Overflow'
I've tried the following settings on pom.xml to use Axis2 wsdl2code: <dependencies> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2</artifactId> <version>1.5.1</version> </dependency> </dependencies> ... <build> … >>> More

• #### Project Euler Question 14 (Collatz Problem)

as seen on Stack Overflow - Search for 'Stack Overflow'
The following iterative sequence is defined for the set of positive integers: n -n/2 (n is even) n -3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 40 20 10 5 16 8 4 2 1 It can be seen that this sequence (starting at 13 and finishing… >>> More