Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,047 changes: 1,047 additions & 0 deletions project/.bloop/scala-build.json

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions src/main/scala/Mathematics/Abs.scala
Original file line number Diff line number Diff line change
@@ -5,13 +5,9 @@ object Abs {
/**
* Method returns absolute value of the number
*
* @param Int
* @return
* @param number the number to find the abs value for
* @return the abs value of number
*/

def abs(number : Int): Int = {
if (number < 0)
return number * -1
return number;
}
def abs(number : Int): Int = if (number < 0) number * -1 else number
}
18 changes: 9 additions & 9 deletions src/main/scala/Mathematics/AbsMax.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package Mathematics

object AbsMax {
import math.abs

/**
* Method returns absolute Maximum Element from the list
*
* @param listOfElements
* @return
*/
def abs: Int => Int = Abs.abs
object AbsMax {

def absMax(elements: List[Int]): Int = abs(elements.maxBy(x => abs(x)))
/**
* Method returns absolute Maximum Element from the list
*
* @param elements the list to consider
* @return the absolute value of the maximum element in the list
*/
def absMax(elements : List[Int]): Int = abs(elements.maxBy(x => abs(x)))

}
18 changes: 9 additions & 9 deletions src/main/scala/Mathematics/AbsMin.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package Mathematics

object AbsMin {
import math.abs

/**
* Method returns Absolute minimum Element from the list
*
* @param listOfElements
* @return
*/
def abs: Int => Int = Abs.abs
object AbsMin {

def absMin(elements: List[Int]): Int = abs(elements.minBy(x => abs(x)))
/**
* Method returns Absolute minimum Element from the list
*
* @param elements the list to consider
* @return the absolute value of the minimum element in the list
*/
def absMin(elements : List[Int]): Int = abs(elements.minBy(x => abs(x)))

}
36 changes: 18 additions & 18 deletions src/main/scala/Mathematics/BinaryExponentiation.scala
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package Mathematics

object BinaryExponentiation {
/**
* Method returns the binary exponentiation of a given number
* when base and power are passed the parameters
*
* @param Int , Int
* @return
*/

def binaryExponentiation(base: Int, power: Int): Int = {
if (power == 0) {
return 1
} else if (power % 2 == 1) {
return binaryExponentiation(base, power - 1) * base
} else {
val answer: Int = binaryExponentiation(base, power / 2)
return answer * answer
}
}
/**
* Method returns the binary exponentiation of a given number
when base and power are passed the parameters
*
* @param base the base to use
* @param power the power to raise it to
* @return the binary exponentiation of the given number
*/
def binaryExponentiation(base : Int, power : Int): Int = {
power match {
case _==0 => 1
case _%2==1 => binaryExponentiation(base, power - 1) * base
case _ =>
val answer: Int = binaryExponentiation(base, power / 2)
answer * answer
}
}
}
8 changes: 4 additions & 4 deletions src/main/scala/Mathematics/Fibonacci.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package Mathematics

object Fibonacci {
private val allFibonacci: Stream[Int] = 1 #:: 1 #:: allFibonacci.zip(allFibonacci.tail).map(t => t._1 + t._2)
private val allFibonacci: Stream[Long] = 1L #:: 1L #:: allFibonacci.zip(allFibonacci.tail).map(t => t._1 + t._2)

/**
* Method to use the allFibonacci stream to take the first total numbers
* Using streams is both an easy and efficient way to generate fibonacci numbers (streams are memoized)
*
* @param total
* @return
* @param total is the number of fibonacci numbers to generate
* @return the first total fibonacci numbers
*/
def fibGenerate(total: Int): Seq[Int] = allFibonacci.take(total)
def fibGenerate(total: Int): Seq[Long] = allFibonacci.take(total)
}
14 changes: 7 additions & 7 deletions src/main/scala/Mathematics/FindMax.scala
Original file line number Diff line number Diff line change
@@ -2,11 +2,11 @@ package Mathematics

object FindMax {

/**
* Method returns Max Element from the list
*
* @param listOfElements
* @return
*/
def findMax(elements: List[Int]): Int = elements.foldLeft(elements.head) { (acc, i) => if (acc > i) acc else i }
/**
* Method returns Max Element from the list
*
* @param elements the list to consider
* @return the maximum element in the list
*/
def findMax(elements : List[Int]): Int = elements.foldLeft(elements.head){(acc, i) => if (acc > i) acc else i}
}
14 changes: 7 additions & 7 deletions src/main/scala/Mathematics/FindMin.scala
Original file line number Diff line number Diff line change
@@ -2,11 +2,11 @@ package Mathematics

object FindMin {

/**
* Method returns Minimum Element from the list
*
* @param listOfElements
* @return
*/
def findMin(elements: List[Int]): Int = elements.foldLeft(elements.head) { (acc, i) => if (acc < i) acc else i }
/**
* Method returns Minimum Element from the list
*
* @param elements the list to find the min element for
* @return the minimum element in the list
*/
def findMin(elements : List[Int]): Int = elements.foldLeft(elements.head){(acc, i) => if (acc < i) acc else i}
}
17 changes: 0 additions & 17 deletions src/main/scala/Mathematics/GreaterCommonDivisor.scala

This file was deleted.

18 changes: 18 additions & 0 deletions src/main/scala/Mathematics/GreatestCommonDivisor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Mathematics

import scala.annotation.tailrec

object GreatestCommonDivisor {

/**
* Method returns the Greatest Common Divisor of two numbers n, m
*
* @param n the first number
* @param m the second number
* @return the greatest common divisor of n and m
*/

@tailrec
def gcd(n: Long, m: Long): Long = if (m == 0) n else gcd(m, n % m)

}
14 changes: 7 additions & 7 deletions src/main/scala/Mathematics/LinearSieve.scala
Original file line number Diff line number Diff line change
@@ -4,16 +4,16 @@ object LinearSieve {
/**
* Method returns sequence of prime numbers which all are not greater than n
*
* @param n
* @return
* @param n the number of prime numbers to generate
* @return the first n prime numbers
*/
def getPrimeNumbers(n: Int): Seq[Int] = {
var primeNumbers = Seq.empty[Int]
val lowestPrimeDivisor: Array[Int] = Array.fill(n + 1)(0)
def getPrimeNumbers(n: Int): Seq[Long] = {
var primeNumbers = Seq.empty[Long]
val lowestPrimeDivisor: Array[Long] = Array.fill(n + 1)(0L)
for (i <- 2 to n) {
if (lowestPrimeDivisor(i) == 0) {
lowestPrimeDivisor(i) = i
primeNumbers :+= i
lowestPrimeDivisor(i) = i.toLong
primeNumbers :+= i.toLong
}
var j = 0
while (j < primeNumbers.length && primeNumbers(j) <= lowestPrimeDivisor(i) && i * primeNumbers(j) <= n) {
27 changes: 13 additions & 14 deletions src/main/scala/Mathematics/PrimeFactors.scala
Original file line number Diff line number Diff line change
@@ -2,19 +2,18 @@ package Mathematics

object PrimeFactors {

/**
* Method returns list of prime factors of number n
*
* @param num
* @return
*/
/**
* Method returns list of prime factors of number n
*
* @param number the number to consider
* @return a list containing the prime factors of the number given
*/

def primeFactors(number: Long): List[Long] = {
val exists = (2L to math.sqrt(number).toLong).find(number % _ == 0)
exists match {
case None => List(number)
case Some(factor) => factor :: primeFactors(number / factor)

}
}
def primeFactors(number: Long): List[Long] = {
val exists = (2L to math.sqrt(number).toLong).find(number % _ == 0)
exists match {
case None => List(number)
case Some(factor) => factor :: primeFactors(number / factor)
}
}
}
12 changes: 6 additions & 6 deletions src/main/scala/Mathematics/StreamSieve.scala
Original file line number Diff line number Diff line change
@@ -2,9 +2,9 @@ package Mathematics

object StreamSieve {

private val allPrimes: Stream[Int] = 2 #:: Stream.from(3).filter { c =>
val primesUptoSqrt = allPrimes.takeWhile(p => p <= math.sqrt(c))
!primesUptoSqrt.exists(p => c % p == 0)
private val allPrimes: Stream[Long] = 2L #:: Stream.from(3).filter { c =>
val primesUpToSqrt = allPrimes.takeWhile(p => p <= math.sqrt(c))
!primesUpToSqrt.exists(p => c % p == 0)
}

/**
@@ -13,8 +13,8 @@ object StreamSieve {
* Using streams as opposed to the classic sieve ensures that we stay following functional principles
* and not change states
*
* @param total
* @return
* @param n number of primes to generate
* @return List of the first n primes
*/
def getPrimeNumbers(n: Int): Seq[Int] = allPrimes.take(n)
def getPrimeNumbers(n: Int): Seq[Long] = allPrimes.take(n)
}
9 changes: 5 additions & 4 deletions src/main/scala/Search/BinarySearch.scala
Original file line number Diff line number Diff line change
@@ -35,8 +35,8 @@ object BinarySearch {
else {
val mid: Int = lo + (hi - lo) / 2
arr(mid) match {
case mv if (mv == elem) => mid
case mv if (mv <= elem) => SearchImpl(mid + 1, hi)
case mv if mv == elem => mid
case mv if mv <= elem => SearchImpl(mid + 1, hi)
case _ => SearchImpl(lo, mid - 1)
}
}
@@ -63,13 +63,14 @@ object BinarySearch {
* @param hi - highest value index
* @return
*/
@tailrec
def lowerBound(arr: List[Int], elem: Int, lo: Int, hi: Int): Int = {
if (lo == hi) lo
else {
val m: Int = lo + (hi - lo) / 2
arr(m) match {
case mv if (mv < elem) => lowerBound(arr, elem, m + 1, hi)
case mv if (mv >= elem) => lowerBound(arr, elem, lo, m)
case mv if mv < elem => lowerBound(arr, elem, m + 1, hi)
case mv if mv >= elem => lowerBound(arr, elem, lo, m)
}
}
}
8 changes: 4 additions & 4 deletions src/main/scala/Search/JumpSearch.scala
Original file line number Diff line number Diff line change
@@ -25,22 +25,22 @@ object JumpSearch {
a = b
b = b + floor(sqrt(len)).toInt
if (a >= len) {
return -1
-1
}
}

while (arr(a) < elem) {
a = a + 1
if (a == min(b, len)) {
return -1
-1
}
}

if (arr(a) == elem) {
return a
a
}
else {
return -1
-1
}
}

15 changes: 6 additions & 9 deletions src/main/scala/Search/LinearSearch.scala
Original file line number Diff line number Diff line change
@@ -6,20 +6,17 @@ package Search
object LinearSearch {
/**
*
* @param arr - a sequence of integers
* @param arr - a sequence of integers
* @param elem - a integer to search for in the @args
* @return - index of the @elem or -1 if elem is not fount in the @arr
*/
def linearSearch(arr: List[Int], elem: Int): Int = {
def iter(index: Int): Int = {
if (index != arr.length) {
if (arr(index) != elem) iter(index + 1)
else index
} else
-1
//the functional way, common in scala would be:
//args.indexOf(target)
for (i <- arr.indices if arr(i) == elem) {
i
}

iter(0)
-1
}

}
5 changes: 1 addition & 4 deletions src/main/scala/Sort/BubbleSort.scala
Original file line number Diff line number Diff line change
@@ -12,9 +12,8 @@ object BubbleSort {
def bubbleSort(array: Array[Int]): Array[Int] = {

breakable {
for (i <- 0 to array.length - 1) {
for (i <- array.indices) {
var swap = false

for (j <- 0 to array.length - 2) {
if (array(j) > array(j + 1)) {
val temp = array(j)
@@ -23,13 +22,11 @@ object BubbleSort {
swap = true
}
}

if (!swap) {
break
}
}
}

array
}

30 changes: 17 additions & 13 deletions src/main/scala/Sort/HeapSort.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package Sort

import scala.util.control.Breaks.{break, breakable}

object HeapSort {

/**
* @param array - a sequence of unsorted integers
* @param arr - a sequence of unsorted integers
* @return - sequence of sorted integers @array
*/

@@ -13,22 +15,24 @@ object HeapSort {
def sift(start: Int, count: Int): Unit = {
var root = start

while (root * 2 + 1 < count) {
var child = root * 2 + 1
if (child < count - 1 && sortedArray(child) < sortedArray(child + 1)) {
child += 1
}
if (sortedArray(root) < sortedArray(child)) {
val t = sortedArray(root)
sortedArray(root) = sortedArray(child)
sortedArray(child) = t
root = child
breakable{
while (root * 2 + 1 < count) {
var child = root * 2 + 1
if (child < count - 1 && sortedArray(child) < sortedArray(child + 1)) {
child += 1
}
if (sortedArray(root) < sortedArray(child)) {
val t = sortedArray(root)
sortedArray(root) = sortedArray(child)
sortedArray(child) = t
root = child
}
else break
}
else return
}
}

var count = sortedArray.length
val count = sortedArray.length
var start = count / 2 - 1
var end = count - 1

18 changes: 9 additions & 9 deletions src/main/scala/Sort/InsertionSort.scala
Original file line number Diff line number Diff line change
@@ -4,21 +4,21 @@ object InsertionSort {

/**
*
* @param array - a sequence of unsorted integers
* @param array - a sequence of unsorted integers
* @return - sequence of sorted integers @array
*/

def insertionSort(array: Array[Int]): Array[Int] = {
def insertionSort(array: Array[Int]) : Array[Int] = {

for (i <- 0 to array.length - 1) {
val temp: Int = array(i)
var j = i - 1
while (j >= 0 && temp < array(j)) {
array(j + 1) = array(j)
j -= 1
for(i <- array.indices) {
val temp:Int=array(i)
var j=i-1
while(j>=0 && temp<array(j)) {
array(j+1)=array(j)
j-=1
}

array(j + 1) = temp
array(j+1)=temp
}

array
2 changes: 1 addition & 1 deletion src/main/scala/Sort/MergeSort.scala
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ object MergeSort {
}

def merge(array: Array[Int], low: Int, mid: Int, high: Int): Array[Int] = {
// copy subarrays
// copy sub arrays
val left = array.slice(low, mid + 1)
val right = array.slice(mid + 1, high + 1)

1 change: 0 additions & 1 deletion src/main/scala/Sort/RecursiveInsertionSort.scala
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@ object RecursiveInsertionSort {
case x2 :: xs2 => if (x <= x2) x :: xs else x2 :: ins(x, xs2)
}
}

insertion(array)
}
}
10 changes: 5 additions & 5 deletions src/main/scala/Sort/SelectionSort.scala
Original file line number Diff line number Diff line change
@@ -9,21 +9,21 @@ object SelectionSort {
*/
def selectionSort(array: Array[Int]): Array[Int] = {

for (i <- 0 to array.length - 1) {
for(i <- array.indices) {

var min: Int = i
var minVal = array(i)

for (j <- i + 1 to array.length - 1) {
if (array(j) < minVal) {
for(j <- i + 1 until array.length) {
if(array(j) < minVal) {
min = j
minVal = array(j)
}
}

val temp: Int = array(i)
array(i) = array(min)
array(min) = temp
array(i)=array(min)
array(min)=temp

}

Original file line number Diff line number Diff line change
@@ -2,14 +2,14 @@ package Mathematics

import org.scalatest.FlatSpec

class GreaterCommonDivisorSpec extends FlatSpec {
class GreatestCommonDivisorSpec extends FlatSpec {

"GreaterCommonDivisorSpec 1" should "output the correct Integer as a result Greatest Common Divisor of two numbers" in {
assert(GreaterCommonDivisor.gcd(80, 10) === 10)
assert(GreatestCommonDivisor.gcd(80, 10) === 10)
}

"GreaterCommonDivisorSpec 2" should "output the correct Integer as a result Greatest Common Divisor of two numbers" in {
assert(GreaterCommonDivisor.gcd(7, 95) === 1)
assert(GreatestCommonDivisor.gcd(7, 95) === 1)
}

}
4 changes: 2 additions & 2 deletions src/test/scala/Mathematics/LinearSieveSpec.scala
Original file line number Diff line number Diff line change
@@ -13,8 +13,8 @@ class LinearSieveSpec extends FlatSpec {
LinearSieve.getPrimeNumbers(n).foreach(x => assert(isPrime(x)))
}

def isPrime(n: Int): Boolean = {
for (i <- 2 until n) if (n % i == 0) return false
def isPrime(n: Long): Boolean = {
for (i <- 2 until n) if (n % i == 0) false
true
}
}