Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made some changes #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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) {
Expand Down
27 changes: 13 additions & 14 deletions src/main/scala/Mathematics/PrimeFactors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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)
}

/**
Expand All @@ -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
Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/Search/JumpSearch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
15 changes: 6 additions & 9 deletions src/main/scala/Search/LinearSearch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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)
Expand All @@ -23,13 +22,11 @@ object BubbleSort {
swap = true
}
}

if (!swap) {
break
}
}
}

array
}

Expand Down