Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
/**
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2013 Lukáš Karas <lukas.karas@centrum.cz>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*/
/**
* This is a small useless example that prints prime numbers less that 1000, written in Scala.
*
* scalac PrimeNumbers.scala && java -classpath .:scala-library-2.9.2.jar PrimeNumbers
*/
object PrimeNumbers {
def primeTest(x: Stream[Int], y: Int): Boolean= {
if (x.isEmpty) {
true
} else {
//println(y + "%" + x.head )
if (y % x.head == 0) {
false
}
else {
primeTest(x.tail, y)
}
}
}
def isPrime2(i:Int ): Boolean = {
//println("is " + i + " prime?")
if (i!=2 && i%2==0)
false
else
primeTest((3 to (Math.sqrt(i).toInt) by 2).toStream, i)
}
def isPrime(i: Int): Boolean = {
if (i <= 3) {
true
} else {
if (i % 2 == 0) {
//println(i + " je sude cislo")
false
} else {
//println(i + " NENI sude cislo")
var sqrt = Math.sqrt(i).toInt
var j = 3
while (j <= sqrt) {
if (j == sqrt)
return true
else if (i % j == 0)
return false
j += 2
}
true
//throw new IllegalStateException("This should never happen")
}
}
}
def main(args: Array[String]) {
(1 to 1000).map(i => {
if (isPrime2(i))
println(i)
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.