# Archive for the ‘Euler’ Category

The deck-of-cards enumeration keeps coming up over and over again in #swift-lang because of the Swift Programming Language guided tour. I recently decided to create my own deck. I started on this because I wanted to test out some of GameplayKit features, namely shuffling, but also because I wanted to play a bit with CustomStringConvertible and CustomPlaygroundQuickLookable.

My goal was actually to build card views, shuffle a deck, and fan out the results. What I found was that the Unicode deck characters won’t render properly to UIKit contexts, where GameplayKit is freely available, and will to OS X ones, where GameplayKit isn’t. (I’m still primarily on Yosemite, not El Capitan.)

I filed some radars, created a post, and put my implementation to the side. Then Maximilian tweeted.

So here’s a developer challenge. Have at the deck enumeration, have some fun, and share your implementation on github. When you’re ready to peek, here’s mine.

Today’s challenge is Euler problem #2. I wanted to start with a less intense problem yesterday because I felt that despite its low Project Euler number, that Problem 2 has a lot more interesting things going for it. Here’s the basic problem:

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

When I first solved this problem as I was just learning Swift, I did so in a fairly obvious way, which is as you see here:

```var fib = [Int]()
fib += 1
fib += 2
var sum = 2
while fib[fib.count - 1] < 4000000 {
fib += fib[fib.count - 2] + fib[fib.count - 1]
let lastItem = fib[fib.count - 1]
if lastItem < 4000000 {
if lastItem % 2 == 0 {
sum += lastItem
}
}
}
sum```

But coming back to it later, I was inspired to completely re-think the problem, to try to, how do I put this?, make it Swiftier, put more Swift in it, Swifticize it.

Here’s what I ended up with:

Today’s challenge is Euler problem #4. I like this one in particular because of the fun I had with the string palindrome function. Got a better solution or a tweak? Drop it into the comments, tweet me, or email me.

```// Project Euler: Problem 4
// https://projecteuler.net/problem=4

// A palindromic number reads the same both ways. The largest palindrome made from the
// product of two 2-digit numbers is 9009 = 91 × 99.
// Find the largest palindrome made from the product of two 3-digit numbers.

func isPalindrome(s : String) -> Bool {
let n = countElements(s)
if n < 2 {return true}
if s[s.startIndex] == s[advance(s.endIndex, -1)] {
return isPalindrome(dropLast(dropFirst(s)))}
return false
}

func getPalindrome () -> String {
for d3a in stride(from: 9, through: 1, by: -1) {
for d3b in stride(from: 9, through: 1, by: -1) {
for d2a in stride(from: 9, through: 0, by: -1) {
for d2b in stride(from: 9, through: 0, by: -1) {
for d1a in stride(from: 9, through: 1, by: -1) { // Thanks ChristianGeek
for d1b in stride(from: 9, through: 1, by: -1) {
let first = d3a * 100 + d2a * 10 + d1a
let second = d3b * 100 + d2b * 10 + d1b
let product = first * second
if  isPalindrome(String(product)) {
return String(product)
}
}
}
}
}
}
}