Skip to content

Instantly share code, notes, and snippets.

@davidgraca
Created May 12, 2017 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidgraca/72b2912f148ed67b686a33f12531e4e9 to your computer and use it in GitHub Desktop.
Save davidgraca/72b2912f148ed67b686a33f12531e4e9 to your computer and use it in GitHub Desktop.
Eurler
import math._
import scala.util._
object Solution extends App {
val n = 1
val horses = (for(i <- 0 until n) yield readInt).toList
println(horses.sorted.
sliding(2).
map(w => w(1) - w(0)).
reduceLeft(_ min _))
}
class Complex(real: Double, imaginary: Double) {
def re() = real
def im() = imaginary
override def toString() =
"" + re + (if (im < 0) "" else "+") + im + "i"
}
object ComplexNumbers {
def main(args: Array[String]) {
val c = new Complex(1.2, 3.4)
println("imaginary part: " + c.im())
}
}
object euler {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
(3 until 1000).filter(n => n % 3 == 0 || n % 5 == 0).sum
//> res0: Int = 233168
val res =for{
n <- 1 to 1000 if n % 3 == 0 || n % 5 == 0
} yield n //> res : scala.collection.immutable.IndexedSeq[Int] = Vector(3, 5, 6, 9, 10, 1
//| 2, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45, 48, 50, 51, 5
//| 4, 55, 57, 60, 63, 65, 66, 69, 70, 72, 75, 78, 80, 81, 84, 85, 87, 90, 93, 9
//| 5, 96, 99, 100, 102, 105, 108, 110, 111, 114, 115, 117, 120, 123, 125, 126,
//| 129, 130, 132, 135, 138, 140, 141, 144, 145, 147, 150, 153, 155, 156, 159, 1
//| 60, 162, 165, 168, 170, 171, 174, 175, 177, 180, 183, 185, 186, 189, 190, 19
//| 2, 195, 198, 200, 201, 204, 205, 207, 210, 213, 215, 216, 219, 220, 222, 225
//| , 228, 230, 231, 234, 235, 237, 240, 243, 245, 246, 249, 250, 252, 255, 258,
//| 260, 261, 264, 265, 267, 270, 273, 275, 276, 279, 280, 282, 285, 288, 290,
//| 291, 294, 295, 297, 300, 303, 305, 306, 309, 310, 312, 315, 318, 320, 321, 3
//| 24, 325, 327, 330, 333, 335, 336, 339, 340, 342, 345, 348, 350, 351, 354, 35
//| 5, 357, 360, 363, 365, 366, 369, 370, 372, 375, 378, 380, 381, 384, 385, 387
//| , 390, 393, 395, 396, 39
//| Output exceeds cutoff limit.
res.sum //> res1: Int = 234168
}
import scala.math.BigInt
object fibbo {
def fib(a:BigInt, b:BigInt):Stream[BigInt] = a #:: fib(b,a+b)
//> fib: (a: scala.math.BigInt, b: scala.math.BigInt)Stream[scala.math.BigInt]
fib(1,2).filter(s=> s % 2 ==0).takeWhile(p=> p <=4000000 ).sum
//> res0: scala.math.BigInt = 4613732
}
import scala.math
object largestprime {
val initNumber:Long = 16L //> initNumber : Long = 16
def IsPrimeNumber(p:Long):Boolean = {
if(p < 10){ p == 2 || p== 3 || p== 5 || p== 7}
else{ p % 2 != 0 && p % 3 != 0 && p % 5 != 0 && p % 7 != 0 }
} //> IsPrimeNumber: (p: Long)Boolean
def stream(i: Long = 1): Stream[Long] = i #:: stream(i + 1)
//> stream: (i: Long)Stream[Long]
def GetNextPrime(startingNumber:Long) = {
val prime = for {
a <- stream(startingNumber + 1)
if(IsPrimeNumber(a))
} yield a
prime
} //> GetNextPrime: (startingNumber: Long)scala.collection.immutable.Stream[Long]
//|
var number:Long = initNumber //> number : Long = 16
var lastPrime:Long=GetNextPrime(1).head //> lastPrime : Long = 2
while(number!=1){
if(number % lastPrime == 0){
number = number / lastPrime
}
else{
lastPrime=GetNextPrime(lastPrime).head
}
}
lastPrime //> res0: Long = 2
}
import scala.math
object largestpalindrome {
def stream(i:Int, order : Int): Stream[Int] = i #:: stream(i + order, order)
//> stream: (i: Int, order: Int)Stream[Int]
def Palindrome(a:Int):Boolean ={
val value = a.toString()
val inverse = value.reverse
value == inverse
} //> Palindrome: (a: Int)Boolean
val pal = for{
a <- stream(999, -1) takeWhile(_>0)
b <- stream(999, -1) takeWhile(_>0)
res = a*b
if(a>=b)
if(Palindrome(res))
} yield res //> pal : scala.collection.immutable.Stream[Int] = Stream(90909, ?)
println(pal.max) //> 906609
}
object euler5 {
def gcd(a:Int,b:Int):Int = if(b == 0) a else gcd(b, a % b)
//> gcd: (a: Int, b: Int)Int
def lcm(a:Int,b:Int):Int = a*b/gcd(a,b) //> lcm: (a: Int, b: Int)Int
(1 until 20).toList.reduceLeft(lcm(_, _)) //> res0: Int = 232792560
// test
(1 until 20).toList.forall(p=> 232792560 % p ==0)
//> res1: Boolean = true
}
object euler6 {
val squares =(1 until 101).toList.map(math.pow(_,2)).reduceLeft(_+_)
//> squares : Double = 338350.0
val sums = math.pow((1 until 101).toList.reduceLeft(_+_),2)
//> sums : Double = 2.55025E7
(squares - sums).toInt //> res0: Int = -25164150
}
import scala.annotation.tailrec
object euler7 {
def stream(i: Long = 1): Stream[Long] = i #:: stream(i + 1)
//> stream: (i: Long)Stream[Long]
def IsPrimeNumber(p:Long):Boolean = {
stream(2).takeWhile(x=> x < p).filter(x=> x * x <= p).forall(v=> p % v > 0)
} //> IsPrimeNumber: (p: Long)Boolean
def GetNextPrime(startingNumber:Long) = {
val prime = for {
a <- stream(startingNumber + 1)
if(IsPrimeNumber(a))
} yield a
prime
} //> GetNextPrime: (startingNumber: Long)scala.collection.immutable.Stream[Long]
//|
def GetNthPrime(limit:Long, lastPrime:Long, carryOver:Int):Long = {
if(limit==carryOver) lastPrime
else GetNthPrime(limit, GetNextPrime(lastPrime).head, carryOver+1)
} //> GetNthPrime: (limit: Long, lastPrime: Long, carryOver: Int)Long
GetNthPrime(10002, 0, 0) //> res0: Long = 104743
}
object euler8 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val strNumber = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
//> strNumber : String = 73167176531330624919225119674426574742355349194934969
//| 835203127745063262395783180169848018694788518438586156078911294949545950173
//| 795833195285320880551112540698747158523863050715693290963295227443043557668
//| 966489504452445231617318564030987111217223831136222989342338030813533627661
//| 428280644448664523874930358907296290491560440772390713810515859307960866701
//| 724271218839987979087922749219016997208880937766572733300105336788122023542
//| 180975125454059475224352584907711670556013604839586446706324415722155397536
//| 978179778461740649551492908625693219784686224828397224137565705605749026140
//| 797296865241453510047482166370484403199890008895243450658541227588666881164
//| 271714799244429282308634656748139191231628245861786645835912456652947654568
//| 284891288314260769004224219022671055626321111109370544217506941658960408071
//| 984038509624554443629812309878799272442849091888458015616609791913387549920
//| 05240636899125607176060
//| Output exceeds cutoff limit.
def MultiplyChars(str:String):Long = {
str.map(_.asDigit.toLong).reduceRight(_ * _)
} //> MultiplyChars: (str: String)Long
val combination=strNumber.sliding(13, 1).maxBy(MultiplyChars)
//> combination : String = 5576689664895
MultiplyChars(combination) //> res0: Long = 23514624000
}
import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._
object FrenchDate {
def main(args: Array[String]) {
val now = new Date
val df = getDateInstance(LONG, Locale.FRANCE)
println(df format now)
}
}
import scala.math
object largestprime {
val initNumber:Long = 16L //> initNumber : Long = 16
def IsPrimeNumber(p:Long):Boolean = {
if(p < 10){ p == 2 || p== 3 || p== 5 || p== 7}
else{ p % 2 != 0 && p % 3 != 0 && p % 5 != 0 && p % 7 != 0 }
} //> IsPrimeNumber: (p: Long)Boolean
def stream(i: Long = 1): Stream[Long] = i #:: stream(i + 1)
//> stream: (i: Long)Stream[Long]
def GetNextPrime(startingNumber:Long) = {
val prime = for {
a <- stream(startingNumber + 1)
if(IsPrimeNumber(a))
} yield a
prime
} //> GetNextPrime: (startingNumber: Long)scala.collection.immutable.Stream[Long]
//|
var number:Long = initNumber //> number : Long = 16
var lastPrime:Long=GetNextPrime(1).head //> lastPrime : Long = 2
while(number!=1){
if(number % lastPrime == 0){
number = number / lastPrime
}
else{
lastPrime=GetNextPrime(lastPrime).head
}
}
lastPrime //> res0: Long = 2
}
import scala.math
object largestpalindrome {
def stream(i:Int, order : Int): Stream[Int] = i #:: stream(i + order, order)
//> stream: (i: Int, order: Int)Stream[Int]
def Palindrome(a:Int):Boolean ={
val value = a.toString()
val inverse = value.reverse
value == inverse
} //> Palindrome: (a: Int)Boolean
val pal = for{
a <- stream(999, -1) takeWhile(_>0)
b <- stream(999, -1) takeWhile(_>0)
res = a*b
if(a>=b)
if(Palindrome(res))
} yield res //> pal : scala.collection.immutable.Stream[Int] = Stream(90909, ?)
println(pal.max) //> 906609
}
object euler5 {
def gcd(a:Int,b:Int):Int = if(b == 0) a else gcd(b, a % b)
//> gcd: (a: Int, b: Int)Int
def lcm(a:Int,b:Int):Int = a*b/gcd(a,b) //> lcm: (a: Int, b: Int)Int
(1 until 20).toList.reduceLeft(lcm(_, _)) //> res0: Int = 232792560
// test
(1 until 20).toList.forall(p=> 232792560 % p ==0)
//> res1: Boolean = true
}
object euler6 {
val squares =(1 until 101).toList.map(math.pow(_,2)).reduceLeft(_+_)
//> squares : Double = 338350.0
val sums = math.pow((1 until 101).toList.reduceLeft(_+_),2)
//> sums : Double = 2.55025E7
(squares - sums).toInt //> res0: Int = -25164150
}
import scala.annotation.tailrec
object euler7 {
def stream(i: Long = 1): Stream[Long] = i #:: stream(i + 1)
//> stream: (i: Long)Stream[Long]
def IsPrimeNumber(p:Long):Boolean = {
stream(2).takeWhile(x=> x < p).filter(x=> x * x <= p).forall(v=> p % v > 0)
} //> IsPrimeNumber: (p: Long)Boolean
def GetNextPrime(startingNumber:Long) = {
val prime = for {
a <- stream(startingNumber + 1)
if(IsPrimeNumber(a))
} yield a
prime
} //> GetNextPrime: (startingNumber: Long)scala.collection.immutable.Stream[Long]
//|
def GetNthPrime(limit:Long, lastPrime:Long, carryOver:Int):Long = {
if(limit==carryOver) lastPrime
else GetNthPrime(limit, GetNextPrime(lastPrime).head, carryOver+1)
} //> GetNthPrime: (limit: Long, lastPrime: Long, carryOver: Int)Long
GetNthPrime(10002, 0, 0) //> res0: Long = 104743
}
object euler8 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val strNumber = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
//> strNumber : String = 73167176531330624919225119674426574742355349194934969
//| 835203127745063262395783180169848018694788518438586156078911294949545950173
//| 795833195285320880551112540698747158523863050715693290963295227443043557668
//| 966489504452445231617318564030987111217223831136222989342338030813533627661
//| 428280644448664523874930358907296290491560440772390713810515859307960866701
//| 724271218839987979087922749219016997208880937766572733300105336788122023542
//| 180975125454059475224352584907711670556013604839586446706324415722155397536
//| 978179778461740649551492908625693219784686224828397224137565705605749026140
//| 797296865241453510047482166370484403199890008895243450658541227588666881164
//| 271714799244429282308634656748139191231628245861786645835912456652947654568
//| 284891288314260769004224219022671055626321111109370544217506941658960408071
//| 984038509624554443629812309878799272442849091888458015616609791913387549920
//| 05240636899125607176060
//| Output exceeds cutoff limit.
def MultiplyChars(str:String):Long = {
str.map(_.asDigit.toLong).reduceRight(_ * _)
} //> MultiplyChars: (str: String)Long
val combination=strNumber.sliding(13, 1).maxBy(MultiplyChars)
//> combination : String = 5576689664895
MultiplyChars(combination) //> res0: Long = 23514624000
}
import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._
object FrenchDate {
def main(args: Array[String]) {
val now = new Date
val df = getDateInstance(LONG, Locale.FRANCE)
println(df format now)
}
}
import scala.math
object HelloWorld {
def main(args: Array[String]) {
def IsPrimeNumber(p:Long):Boolean = {
def IsPrimeAfter5(n:Long) : Boolean = {
val prime = for {
hyp <- Stream.from(5, 6) takeWhile(_ < n)
res=(hyp,n % hyp == 0 || n % (hyp+2) == 0)
if(hyp.toDouble <= math.sqrt(n))
} yield res
println(prime.toList)
false
}
if (p <=1) false
else if(p <= 3) true
else if(p % 2 == 0 || p % 3 ==0) false
IsPrimeAfter5(p)
Stream
}
print(IsPrimeNumber(15))
}}
object test {
println("Welcome to the Scala worksheet")
import scala.annotation._
@tailrec def test(n : Int, acc : Int) : Int =
if (n <= 0)
acc
else
test(n-1, acc+1)
test(10, 0)
}
object test {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
def IsPrimeNumber(p:Long):Boolean = {
def IsPrimeAfter5(n:Long) : Boolean = {
val prime = for{
hyp <- Stream.from(5) takeWhile(_< math.sqrt(n))
if(!(n % hyp ==0 || n % (hyp+2) == 0))
} yield hyp
prime take 1
true
}
if (p <=1) false
else if(p <= 3) true
else if(p % 2 == 0 || p % 3 ==0) false
IsPrimeAfter5(p)
} //> IsPrimeNumber: (p: Long)Boolean
IsPrimeNumber(7) //> res0: Boolean = true
}
object Timer {
def oncePerSecond(callback: () => Unit) {
while (true) { callback(); Thread sleep 1000 }
}
def timeFlies() {
println("time flies like an arrow...")
}
def main(args: Array[String]) {
oncePerSecond(timeFlies)
}
}
object TimerAnonymous {
def oncePerSecond(callback: () => Unit) {
while (true) { callback(); Thread sleep 1000 }
}
def main(args: Array[String]) {
oncePerSecond(() =>
println("time flies like an arrow..."))
}
}
abstract class Tree{
type Environment = (String) => Int
}
case class Sum(l: Tree, r: Tree) extends Tree
case class Var(n: String) extends Tree
case class Const(v: Int) extends Tree
import math._
import scala.util._
object Solution extends App {
val n = 1
val horses = (for(i <- 0 until n) yield readInt).toList
println(horses.sorted.
sliding(2).
map(w => w(1) - w(0)).
reduceLeft(_ min _))
}
class Complex(real: Double, imaginary: Double) {
def re() = real
def im() = imaginary
override def toString() =
"" + re + (if (im < 0) "" else "+") + im + "i"
}
object ComplexNumbers {
def main(args: Array[String]) {
val c = new Complex(1.2, 3.4)
println("imaginary part: " + c.im())
}
}
object euler {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
(3 until 1000).filter(n => n % 3 == 0 || n % 5 == 0).sum
//> res0: Int = 233168
val res =for{
n <- 1 to 1000 if n % 3 == 0 || n % 5 == 0
} yield n //> res : scala.collection.immutable.IndexedSeq[Int] = Vector(3, 5, 6, 9, 10, 1
//| 2, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45, 48, 50, 51, 5
//| 4, 55, 57, 60, 63, 65, 66, 69, 70, 72, 75, 78, 80, 81, 84, 85, 87, 90, 93, 9
//| 5, 96, 99, 100, 102, 105, 108, 110, 111, 114, 115, 117, 120, 123, 125, 126,
//| 129, 130, 132, 135, 138, 140, 141, 144, 145, 147, 150, 153, 155, 156, 159, 1
//| 60, 162, 165, 168, 170, 171, 174, 175, 177, 180, 183, 185, 186, 189, 190, 19
//| 2, 195, 198, 200, 201, 204, 205, 207, 210, 213, 215, 216, 219, 220, 222, 225
//| , 228, 230, 231, 234, 235, 237, 240, 243, 245, 246, 249, 250, 252, 255, 258,
//| 260, 261, 264, 265, 267, 270, 273, 275, 276, 279, 280, 282, 285, 288, 290,
//| 291, 294, 295, 297, 300, 303, 305, 306, 309, 310, 312, 315, 318, 320, 321, 3
//| 24, 325, 327, 330, 333, 335, 336, 339, 340, 342, 345, 348, 350, 351, 354, 35
//| 5, 357, 360, 363, 365, 366, 369, 370, 372, 375, 378, 380, 381, 384, 385, 387
//| , 390, 393, 395, 396, 39
//| Output exceeds cutoff limit.
res.sum //> res1: Int = 234168
}
import scala.math.BigInt
object fibbo {
def fib(a:BigInt, b:BigInt):Stream[BigInt] = a #:: fib(b,a+b)
//> fib: (a: scala.math.BigInt, b: scala.math.BigInt)Stream[scala.math.BigInt]
fib(1,2).filter(s=> s % 2 ==0).takeWhile(p=> p <=4000000 ).sum
//> res0: scala.math.BigInt = 4613732
}
import scala.math
object HelloWorld {
def main(args: Array[String]) {
def IsPrimeNumber(p:Long):Boolean = {
def IsPrimeAfter5(n:Long) : Boolean = {
val prime = for {
hyp <- Stream.from(5, 6) takeWhile(_ < n)
res=(hyp,n % hyp == 0 || n % (hyp+2) == 0)
if(hyp.toDouble <= math.sqrt(n))
} yield res
println(prime.toList)
false
}
if (p <=1) false
else if(p <= 3) true
else if(p % 2 == 0 || p % 3 ==0) false
IsPrimeAfter5(p)
Stream
}
print(IsPrimeNumber(15))
}}
object test {
println("Welcome to the Scala worksheet")
import scala.annotation._
@tailrec def test(n : Int, acc : Int) : Int =
if (n <= 0)
acc
else
test(n-1, acc+1)
test(10, 0)
}
object test {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
def IsPrimeNumber(p:Long):Boolean = {
def IsPrimeAfter5(n:Long) : Boolean = {
val prime = for{
hyp <- Stream.from(5) takeWhile(_< math.sqrt(n))
if(!(n % hyp ==0 || n % (hyp+2) == 0))
} yield hyp
prime take 1
true
}
if (p <=1) false
else if(p <= 3) true
else if(p % 2 == 0 || p % 3 ==0) false
IsPrimeAfter5(p)
} //> IsPrimeNumber: (p: Long)Boolean
IsPrimeNumber(7) //> res0: Boolean = true
}
object Timer {
def oncePerSecond(callback: () => Unit) {
while (true) { callback(); Thread sleep 1000 }
}
def timeFlies() {
println("time flies like an arrow...")
}
def main(args: Array[String]) {
oncePerSecond(timeFlies)
}
}
object TimerAnonymous {
def oncePerSecond(callback: () => Unit) {
while (true) { callback(); Thread sleep 1000 }
}
def main(args: Array[String]) {
oncePerSecond(() =>
println("time flies like an arrow..."))
}
}
abstract class Tree{
type Environment = (String) => Int
}
case class Sum(l: Tree, r: Tree) extends Tree
case class Var(n: String) extends Tree
case class Const(v: Int) extends Tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment