Skip to content

Instantly share code, notes, and snippets.

@proxpero
Last active October 9, 2015 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save proxpero/18cc7e49f6aaa35c3588 to your computer and use it in GitHub Desktop.
Save proxpero/18cc7e49f6aaa35c3588 to your computer and use it in GitHub Desktop.
Project Euler problem # 42 in Swift
//: Coded Triangle Numbers
//: [Problem 42](https://projecteuler.net/problem=42)
//: Xcode 7.0, Swift 2.0
/*:
The nth term of the sequence of triangle numbers is given by, t[n] = ½n(n+1); so the first ten triangle numbers are:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t[10]. If the word value is a triangle number then we shall call the word a triangle word.
Using [words.txt](https://projecteuler.net/project/resources/p042_words.txt), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
*/
//: Note: I found and replaced the double quotes directly in the text file rather than in code since escaping the double quotes in a string kept crashing the playground. I named the file "words.txt" in the "Resources" folder of the playground.
import Foundation
// load the words from the file into an array
let url: NSURL! = NSBundle.mainBundle().URLForResource("words", withExtension: "txt")
let words = try String.init(contentsOfURL: url, encoding: NSUTF8StringEncoding).characters.split{$0 == ","}.map(String.init)
extension String {
// an array holding all the triangle numbers determined so far
static var triangleNumbers = [1]
// returns [A, B, C, ... X, Y, Z]
static let alphabet: [String] = (65..<65 + 26).map{String(UnicodeScalar($0))}
var isTriangular: Bool {
// map the characters to their positions in the alphabet and get the sum
let value = self.characters.map { String.alphabet.indexOf(String($0))! + 1 }.reduce(0) { $0 + $1 }
// lazily expand the array of triangle numbers
while value > String.triangleNumbers.last! {
String.triangleNumbers.append(Int(0.5 * Double(String.triangleNumbers.count) * Double((String.triangleNumbers.count + 1))))
}
return String.triangleNumbers.contains(value)
}
}
let triangularWords = words.filter { $0.isTriangular }
let answer = triangularWords.count
// The result was confirmed by projecteuler.net.
// I agreed not to share the answer on the internet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment