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

P31 to P39 problems #49

Open
wants to merge 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added micro/s99/jaranda/.cache
Binary file not shown.
7 changes: 5 additions & 2 deletions micro/s99/jaranda/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ project/boot/
project/plugins/project/

# Scala-IDE specific
.idea
.idea_modules
.scala_dependencies

.settings
.classpath
.project
10 changes: 10 additions & 0 deletions micro/s99/jaranda/.worksheet/src/s99.dummy.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package s99

import s99.Arithmetic._

object dummy {;import org.scalaide.worksheet.runtime.library.WorksheetSupport._; def main(args: Array[String])=$execute{;$skip(81);

println(315.primeFactors);$skip(111); val res$0 =

List(3,3,4,5).groupBy(f => f).map(fl => (fl._1, (fl._1, fl._2.size))).values.toList.sortWith(_._1 < _._1);System.out.println("""res0: List[(Int, Int)] = """ + $show(res$0))}
}
2 changes: 1 addition & 1 deletion micro/s99/jaranda/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.2")
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.4.0")
56 changes: 56 additions & 0 deletions micro/s99/jaranda/src/main/scala/s99/Arithmetic.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package s99

import scala.annotation.tailrec

object Arithmetic {

implicit def int2MyInt(i: Int) = new MyInt(i);

val primes: Stream[Int] = 2 #:: 3 #:: 5 #:: 7 #:: 11 #:: 13 #:: Stream.from(17).filter(_.isPrime)

class MyInt(val i: Int) {

def isPrime: Boolean = {
(2 to math.sqrt(i).toInt).filter(n => i % n == 0).length == 0
}

def isCoprimeTo(n: Int): Boolean = {
gcd(i, n) == 1
}

def totient: Int = {
(1 to i).filter(i.isCoprimeTo(_)).length
}

def totientImproved : Int = {
i.primeFactorMultiplicity.map( n => (n._1 - 1) * math.pow((n._1),(n._2 - 1)) ).reduce( (n , m) => n * m).intValue();
}

def primeFactors: List[Int] = {
@tailrec
def primeFactorsRec(n: Int, ps: Stream[Int], factors: List[Int]) : List[Int] = {
if (n.isPrime) n :: factors
else if (n % ps.head == 0) primeFactorsRec(n / ps.head, ps, ps.head :: factors)
else primeFactorsRec(n, ps.tail, factors)
}
primeFactorsRec(i, primes, Nil).reverse
}

def primeFactorMultiplicity: List[(Int,Int)] = {
val factorsMultiplicity = i.primeFactors.groupBy(f => f).map(f1 => (f1, (f1._1, f1._2.size))).values.toList
factorsMultiplicity.sortWith(_._1 < _._1)
}

}

def gcd(a: Int, b: Int) : Int = (a,b) match {
case (a, 0) => a
case _ => gcd(b, a%b)
}

def listPrimesInRange(r: Range) : List[Int] = {
primes.dropWhile(_ < r.head).takeWhile(_ <= r.last).toList
}

}

8 changes: 0 additions & 8 deletions micro/s99/jaranda/src/main/scala/s99/Lists.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ package s99

import scala.annotation.tailrec

/**
* Created with IntelliJ IDEA.
* User: jaranda
* Date: 05/01/14
* Time: 09:47
* To change this template use File | Settings | File Templates.
*/

object Lists {

/**
Expand Down
14 changes: 14 additions & 0 deletions micro/s99/jaranda/src/main/scala/s99/dummy.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package s99

import s99.Arithmetic._

object dummy {

println(315.primeFactors) //> List(3, 3, 5, 7)

List(3,3,4,5).groupBy(f => f).map(fl => (fl._1, (fl._1, fl._2.size))).values.toList.sortWith(_._1 < _._1)
//> res0: List[(Int, Int)] = List((3,2), (4,1), (5,1))

listPrimesInRange(7 to 31) //> res1: List[Int] = List(7, 11, 13, 17, 19, 23, 29, 31)

}
41 changes: 41 additions & 0 deletions micro/s99/jaranda/src/test/scala/s99/ArithmeticSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package s99

import org.scalatest.FunSuite

class ArithmeticSpec extends FunSuite {

import s99.Arithmetic._;

test("Determine whether a given integer number is prime.") {
assert{ 7.isPrime == true }
}

test("Determine the greatest common divisor of two positive integer numbers.") {
assert{ gcd(36, 63) == 9 }
}

test("Determine whether two positive integer numbers are coprime.") {
assert{ 35.isCoprimeTo(64) }
}

test("Calculate Euler's totient function phi(m).") {
assert{ 10.totient == 4 }
}

test("Determine the prime factors of a given positive integer.") {
assert{ 315.primeFactors == List(3, 3, 5, 7) }
}

test("Determine the prime factors of a given positive integer (2).") {
assert{ 315.primeFactorMultiplicity == List((3,2), (5,1), (7,1)) }
}

test("Calculate Euler's totient function phi(m) (improved)."){
assert{ 10.totientImproved == 4 }
}

test("Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range.") {
assert{ listPrimesInRange(7 to 31) == List(7, 11, 13, 17, 19, 23, 29, 31) }
}

}
8 changes: 0 additions & 8 deletions micro/s99/jaranda/src/test/scala/s99/Lists.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ package s99
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.FlatSpec

/**
* Created with IntelliJ IDEA.
* User: jaranda
* Date: 05/01/14
* Time: 10:27
* To change this template use File | Settings | File Templates.
*/

class ListsSpec extends FlatSpec with ShouldMatchers {

"Lists problems set" should "find the last element of a list" in {
Expand Down