Skip to content

Commit

Permalink
Pi estimation example job in Scala
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostas Tzoumas authored and aljoscha committed Sep 22, 2014
1 parent 0dc7614 commit 299cef7
Showing 1 changed file with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.examples.scala.misc

import org.apache.flink.api.scala._

object PiEstimation {

def main(args: Array[String]) {

val numSamples: Long = if (args.length > 0) args(0).toLong else 1000000

val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment

// count how many of the samples would randomly fall into
// the unit circle
val count =
env.generateSequence(1, numSamples)
.map (sample => {
val x = Math.random()
val y = Math.random()
if (x * x + y * y < 1) 1L else 0L
})
.reduce(_+_)

// the ratio of the unit circle surface to 4 times the unit square is pi
val pi = count
.map (_ * 4.0 / numSamples)

println("We estimate Pi to be:")

pi.print()

env.execute("PiEstimation example")
}

}

0 comments on commit 299cef7

Please sign in to comment.