-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsPrimeAndSums.sc
69 lines (52 loc) · 1.09 KB
/
IsPrimeAndSums.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import scala.annotation.tailrec
def double(n: Int): Int = n + n
(n: Int) => n+n
val double2 = double _
def sumAndProduct(start: Int, end: Int): Either[Integer, Integer] = {
Left (start+end)
Right (start*end)
}
def sumAndProduc2t(start: Int, end: Int): (Int, Int) = {
val TwoValues = (2,3) : (Int, Int)
TwoValues
}
def isPrime2(z: Int, x: Int, y: Int): Boolean = {
var yret = y
if(x==1) {
if (y > 1)
return false
else
return true
}
if(z%x==0&&x!=0) {
yret = y + 1
}
return isPrime2(z, x-1, yret)
}
def isPrime(z: Int): Boolean = {
return isPrime2(z, z, 0)
}
def isPrime3(n: Int) : Boolean = {
def ip(d: Int): Boolean = {
if(d==n) true
else if(n%d == 0) false
else ip(d+1)
}
if(n<2) false
else ip(2)
}
def sumInterval(start: Int, end: Int): (Int, Int) = {
if(start>=end) (0,1)
else {
val result = sumInterval(start+1, end);
(result._1+start, result._2*start)
}
}
double(3)
double2(2)
sumAndProduc2t(2,3)._1
sumAndProduc2t(2,3)._2
//sumAndProduct(2,3).left
//sumAndProduct(2,3).right
isPrime(12)
sumInterval(1,4)