Skip to content

Instantly share code, notes, and snippets.

@harrisonweinerman
Last active August 29, 2015 14:21
Show Gist options
  • Save harrisonweinerman/b2c60453708240d941ea to your computer and use it in GitHub Desktop.
Save harrisonweinerman/b2c60453708240d941ea to your computer and use it in GitHub Desktop.
Swift Riemann Sum Calculator
//This code works fine in a Swift playground.
let startingX = 0.0
let finishingX = 2.0
//make this smaller for more accuracy but slower calculation times
let increment = 0.1
var area = 0.0
func fOfX(x : Double) -> Double{
//write your function here
return 8 - pow(x, 2)
}
for var i = startingX; i < finishingX; i += increment {
area += increment * fOfX(i)
}
println("Area is \(area).")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment