Created
June 1, 2019 15:47
-
-
Save vvviiimmm/3eecdcbd0e7c2b076a5601ebd6cdbcc6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.spark.{SparkConf, SparkContext} | |
import scala.util.{Failure, Success, Try} | |
object Main extends App { | |
def createSparkContext: SparkContext = { | |
val conf = new SparkConf().setMaster("local[*]").setAppName("Factorial") | |
val sc = new SparkContext(conf) | |
sc.setLogLevel("ERROR") | |
sc | |
} | |
def factorial(sparkContext: SparkContext, num: BigInt): BigInt = | |
if (num == 0) BigInt(1) | |
else { | |
val list = (BigInt(1) to num).toList | |
sparkContext.parallelize(list).reduce(_ * _) | |
} | |
print("Please enter a number: ") | |
Try(scala.io.StdIn.readLine().toInt) match { | |
case Success(n) => | |
val context = createSparkContext | |
val fact = factorial(context, n) | |
println(s"Factorial of $n is $fact") | |
case Failure(_) => | |
println("Invalid number") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment