Slide 1

Slide 1 text

LEARNING SWIFT THE ULER WAY

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

PROJECT EULER ~500 MATH PROBLEMS THAT RANGE FROM EASY TO INSANELY DIFFICULT

Slide 6

Slide 6 text

PROBLEM #1 FIND THE SUM OF ALL THE MULTIPLES OF 3 OR 5 BELOW 1000

Slide 7

Slide 7 text

PROBLEM #454 DIOPHANTINE RECIPROCALS III IN THE FOLLOWING EQUATION , , AND ARE POSITIVE INTEGERS. FOR A LIMIT WE DEFINE F( ) AS THE NUMBER OF SOLUTIONS WHICH SATISFY . WE CAN VERIFY THAT F(15) = 4 AND F(1000) = 1069.

Slide 8

Slide 8 text

FIND F( )

Slide 9

Slide 9 text

FIND F(1 TRILLION)

Slide 10

Slide 10 text

FIND F(5 )

Slide 11

Slide 11 text

FIND F( )

Slide 12

Slide 12 text

SOME THINGS I'M NOT REALLY GREAT AT...

Slide 13

Slide 13 text

SWIFT FUNCTIONAL PROGRAMMERING MATH

Slide 14

Slide 14 text

SOME THINGS I EXCELL AT...

Slide 15

Slide 15 text

OPENING BROWSER TABS HITTING ⌘-U AND ⌘-R TIMES

Slide 16

Slide 16 text

PROBLEM #1 FIND THE SUM OF ALL THE MULTIPLES OF 3 OR 5 BELOW 1000.

Slide 17

Slide 17 text

extension Int { func isMultipleOf(i: Int) -> Bool { return i != 0 && self % i == 0 } }

Slide 18

Slide 18 text

extension Int { func isMultipleOf(i: Int) -> Bool { return i != 0 && self % i == 0 } func isMultipleOfAny(nums:[Int]) -> Bool { for i in nums { if self.isMultipleOf(i) { return true } } return false } }

Slide 19

Slide 19 text

WHAT IS FUNCTIONAL PROGRAMMING?

Slide 20

Slide 20 text

Functional programming is programming without assignment statements. — Uncle Bob

Slide 21

Slide 21 text

extension Int { func isMultipleOf(i: Int) -> Bool { return i != 0 && self % i == 0 } func isMultipleOfAny(nums:[Int]) -> Bool { return nums.map(isMultipleOf).reduce(false) {$0 || $1} } }

Slide 22

Slide 22 text

let sum = (1...999).filter{ $0.isMultipleOfAny([3,5]) }.reduce(0, combine:+)

Slide 23

Slide 23 text

SOME FUN THINGS...

Slide 24

Slide 24 text

SEQUENCES

Slide 25

Slide 25 text

struct FibonacciSequence: SequenceType { func generate() -> AnyGenerator { var last = 0, current = 1 return anyGenerator { let next = last + current last = current current = next return next } } }

Slide 26

Slide 26 text

struct PrimeSequence: SequenceType { func generate() -> AnyGenerator { var currentPrime = 1, nextPrime = 1 return anyGenerator { nextPrime = currentPrime+1 while !nextPrime.isPrime() { nextPrime += 1 } currentPrime = nextPrime return nextPrime } } }

Slide 27

Slide 27 text

WHAT ABOUT? // // There are infinite prime numbers. Math is tricky like that. // for p in PrimeSequence() { print(p) }

Slide 28

Slide 28 text

struct LimitSequence: SequenceType { let test: (Int,T) -> Bool let sequence: S init(sequence:S, test: (Int,T) -> Bool) { self.sequence = sequence self.test = test } func generate() -> AnyGenerator { var generator = self.sequence.generate() var counter:Int = 0 return anyGenerator { let next = generator.next() if next != nil && self.test(++counter, next!) { return next } return .None } } }

Slide 29

Slide 29 text

PROBLEM 2 // // By considering the terms in the Fibonacci sequence whose values do not // exceed four million, find the sum of the even-valued terms. // let seq = LimitSequence(sequence: FibonacciSequence()) { $1 < 4_000_000 } let sum = seq.filter(isEven).reduce(0,combine: +)

Slide 30

Slide 30 text

PROBLEM 7 // // What is the 10,001st prime number? // // If I use $0 here, compiler complains. So, explicitly using "(i,_) -> Bool" let primes = LimitSequence(sequence:PrimeSequence()) { (i, _) -> Bool in i < 10_001 } let lastPrime = Array(primes).last

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

WWDC 2015 - PROTOCOL-ORIENTED PROGRAMMING IN SWIFT

Slide 33

Slide 33 text

extension SequenceType { typealias ValueTest = (Self.Generator.Element) -> Bool func take(i:Int) -> LimitSequence { return LimitSequence(sequence: self) { (counter,_) -> Bool in i < counter } } func takeWhile(test:ValueTest) -> LimitSequence { return LimitSequence(sequence: self) { test($1) } } func last() -> Self.Generator.Element { return Array(self).last! } }

Slide 34

Slide 34 text

PROBLEM 2 (CRUSTIFIED) // // By considering the terms in the Fibonacci sequence whose values do not // exceed four million, find the sum of the even-valued terms. // let sum = FibonacciSequence().takeWhile({ $0 < 4_000_000 }).filter(isEven).reduce(0,combine: +)

Slide 35

Slide 35 text

PROBLEM 7 (CRUSTIFIED) // // What is the 10,001st prime number? // let lastPrime = PrimeSequence().take(10_001).last()

Slide 36

Slide 36 text

BIGNUM

Slide 37

Slide 37 text

PROBLEM 20 // // n! means n × (n − 1) × ... × 3 × 2 × 1 // // For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, // and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. // // Find the sum of the digits in the number 100! //

Slide 38

Slide 38 text

// WILL NOT WORK let total:Int = 9332621544394415268169923885626670049071596826438162146859 2963895217599993229915608941463976156518286253697920827223 758251185210916864000000000000000000000000

Slide 39

Slide 39 text

ATTEMPT 1: STRINGNUM // // multiplyString does what we learned in gradeschool // let total = (2...100).reduce("1") { multiplyString($0, times: $1) } print("sum = \(sumOfDigits(total))")

Slide 40

Slide 40 text

PROBLEM 25 WHAT IS THE INDEX OF THE FIRST TERM IN THE FIBONACCI SEQUENCE TO CONTAIN 1000 DIGITS?

Slide 41

Slide 41 text

struct StringFibonacciSequence: SequenceType { func generate() -> AnyGenerator { var last = "0", current = "1" return anyGenerator { let next = addStringInteger(last, rhs: current) last = current current = next return next } } }

Slide 42

Slide 42 text

ATTEMPT 2: JKBIGNUM struct BigNumFibonacciSequence: SequenceType { func generate() -> AnyGenerator { var last = JKBigInteger(string:"0"), current = JKBigInteger(string:"1") return anyGenerator { let next = last + current last = current current = next return next } } }

Slide 43

Slide 43 text

XCTEST PERFORMANCE TESTS

Slide 44

Slide 44 text

func testIsPalindromePerformance() { self.measureBlock() { for i in 0...10000 { isPalindrome(995000 + i) } } }

Slide 45

Slide 45 text

SWIFT FUNCTIONAL PROGRAMMERING MATH

Slide 46

Slide 46 text

Mostly SWIFT FUNCTIONAL PROGRAMMERING MATH

Slide 47

Slide 47 text

Mostly SWIFT Lots of FUNCTIONAL PROGRAMMERING MATH

Slide 48

Slide 48 text

Mostly SWIFT Lots of FUNCTIONAL PROGRAMMERING OMG MATH is hard

Slide 49

Slide 49 text

These are hard problems, but they are known to be solveable.

Slide 50

Slide 50 text

WHAT'S NEXT FOR ME? ▸ still not heavy production Swift ▸ prototype in Swift ▸ nonnull/nullable

Slide 51

Slide 51 text

WHAT'S NEXT FOR YOU? ▸ Try these out ▸ I took out the answers HTTPS://GITHUB.COM/DBGRANDI/EULERKIT

Slide 52

Slide 52 text

Questions?

Slide 53

Slide 53 text

I'm @dbgrandi Stay Crusty

Slide 54

Slide 54 text

Links Euler: http://projecteuler.net Uncle Bob Quote: https://pragprog.com/magazines/2013-01/functional-programming-basics EulerKit: https://github.com/dbgrandi/EulerKit Protocol Oriented Programming: https://developer.apple.com/videos/wwdc/2015/?id=408 JKBigInteger: https://github.com/kirsteins/JKBigInteger Images Not a dinosaur: http://www.kappit.com/img/pics/201408_2234_aechh_sm.jpg Ruby on Rails: https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Ruby_on_Rails_logo.svg/2000px-Ruby_on_Rails_logo.svg.png Ruby: https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Ruby_logo.svg/1024px-Ruby_logo.svg.png Milky Way Galaxy: http://www.nasa.gov/images/content/63376main_image_feature_202_jwfull.jpg Milky Way Chocolate: https://upload.wikimedia.org/wikipedia/en/5/5e/Small-milky-way-package.jpg Krusty the Clown: https://upload.wikimedia.org/wikipedia/en/5/5a/Krustytheclown.png Crusty: screenshot from WWDC Video (above)