Snippets

lion 137 Scala

Created by lion 137 last modified
class Rational(x: Int, y: Int) {
  require(y != 0, "deniminator must be nonzero")
  
  def this(x: Int) = this(x, 1)
  private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
  private val g = gcd(x, y)

  def numer = x / g // if numer and denom are called very often we may put: val numer = x / gcd(x,y).....
  def denom = y / g

  def <(that: Rational) = this.numer * that.denom < that.numer * this.denom

  def max(that: Rational) = if (this < that) that else this

  def  + (that: Rational) =
    new Rational(
      numer * that.denom + that.numer * denom,
      denom * that.denom)

  def unary_- : Rational =
    new Rational(-numer, denom)

  def  - (that: Rational) = this + -that
  
  def square () = denom * denom
    

  override def toString = numer + "/" + denom
}
//euler with square factoring https://projecteuler.net/problem=152

import scala.io.StdIn.{ readLine, readInt }
import scala.collection.mutable.{ MutableList }
object Main extends App {


	val r1 = new Rational(4, 2)
	val r2 = new Rational(8, 4)
	//println(r1)
	println(r1 === r2)
                                                 
  
	
  
  def sumOfPower(M1: Int, N: Int): Int = {
  	def even(a: Int) = if (a % 2 == 0) true else false
                                                  
	def square(a: Int) = a * a                
	
	def max(x: Int, y: Int) = if (x >= y) x else y
                                                  
	def min(x: Int, y: Int) = if (x < y) x else y
                                                  
 
 
  def power(a: Int, b:Int): Int = {
  	if (b == 0) 1
  	else if (even(b))  square(power(a, b / 2))
  	else
  		a * (power(a, b - 1))
  }                                            
  
  def minArgument(x: Int, y: Int): Int = {
    var s = 0
    var i = 1
    while (s < x) {
      s = s + power(i, y)
            i = i + 1
		}
    i - 1
  }                                             
  
 
  def maxPower(a: Int): Int = math.sqrt(a).floor.toInt
  	val K1 = maxPower(M1)
  	
  	def f(M: Int, K: Int): Int = {
  		if (M < 1) 0;
  		if (M == 1) 1;
  		if ((M == 2) || (M == 3)) 0;
  		var lowerLimit = minArgument(M, N)
  		if (K < lowerLimit) 0;
  		else{
  		if (power(K, N) == M) 1 + f(M, K -1)
  		else {
  			f(M - power(K, N), min(maxPower(M - power(K, N)), K-1)) + f(M , K -1)
  		}
  }
  }
  f(M1, K1)
 
 } 	
}
class Rational(x: Int, y: Int) {
  require(y != 0, "deniminator must be nonzero")
  
  def this(x: Int) = this(x, 1)
  private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
  private val g = gcd(x, y)

  def numer = x / g // if numer and denom are called very often we may put: val numer = x / gcd(x,y).....
  def denom = y / g

  def <(that: Rational) = this.numer * that.denom < that.numer * this.denom
  def ===(that: Rational) = this.numer * that.denom == that.numer * this.denom

  def max(that: Rational) = if (this < that) that else this

  def  + (that: Rational) =
    new Rational(
      numer * that.denom + that.numer * denom,
      denom * that.denom)

  def unary_- : Rational =
    new Rational(-numer, denom)

  def  - (that: Rational) = this + -that
  
  def square () = denom * denom
    

  override def toString = numer + "/" + denom
}
1
2
3
4
5
6
7
  def listLengthIter(x: List[Int]) : Int = {
  	def helper(xs: List[Int], n: Int) : Int = {
  		if (xs.isEmpty) return n
  			else
  				return (helper(xs.tail, n + 1))}
  		return (helper(x, 0))
  }

import scala.io.StdIn.{ readLine, readInt }
import scala.collection.mutable.{ MutableList }
object sumOfPowersTets {

	
                                                 
  

  
  def sumOfPower(M1: Int, N: Int): Int = {
  	def even(a: Int) = if (a % 2 == 0) true else false
                                                  
	def square(a: Int) = a * a                
	
	def max(x: Int, y: Int) = if (x >= y) x else y
                                                  
	def min(x: Int, y: Int) = if (x < y) x else y
                                                  
 
 
  def power(a: Int, b:Int): Int = {
  	if (b == 0) 1
  	else if (even(b))  square(power(a, b / 2))
  	else
  		a * (power(a, b - 1))
  }                                            
  
  def minArgument(x: Int, y: Int): Int = {
    var s = 0
    var i = 1
    while (s < x) {
      s = s + power(i, y)
            i = i + 1
		}
    i - 1
  }                                             
  
 
  def maxPower(a: Int): Int = math.sqrt(a).floor.toInt
  	val K1 = maxPower(M1)
  	
  	def f(M: Int, K: Int): Int = {
  		if (M < 1) 0;
  		if (M == 1) 1;
  		if ((M == 2) || (M == 3)) 0;
  		var lowerLimit = minArgument(M, N)
  		if (K < lowerLimit) 0;
  		else{
  		if (power(K, N) == M) 1 + f(M, K -1)
  		else {
  			f(M - power(K, N), min(maxPower(M - power(K, N)), K-1)) + f(M , K -1)
  		}
  }
  }
  f(M1, K1)
 
 }                                            
 def main(args: Array[String]) {

    println(sumOfPower(readInt(), readInt()))
   
 }
 	
 }
package greeter

import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer

object tests {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
 // val z = 1 to 1000
  
//  val L1 = List(z)
  val L2 = List(1, 2, 3, 4)                       //> L2  : List[Int] = List(1, 2, 3, 4)
  
  def listTestA() ={
    var list1:List[Int] = Nil

    for(i <- 0 to 30000)
        list1 = list1 ::: List(i)
    list1
}                                                 //> listTestA: ()List[Int]
  listTestA()                                     //> res0: List[Int] = List(0, 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)
  var L3 = listTestA()                            //> L3  : List[Int] = List(0, 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)
  def myListLength(x: List[Int]) : Int = {
  	if (x.isEmpty) return 0
  		else
  		return (myListLength(x.tail) + 1)
 
   }                                              //> myListLength: (x: List[Int])Int
 //  myListLength(L3)                             //> res1: Int = 31
  def listLengthIter(x: List[Int]) : Int = {
  	def helper(xs: List[Int], n: Int) : Int = {
  		if (xs.isEmpty) return n
  			else
  				return (helper(xs.tail, n + 1))}
  		return (helper(x, 0))
  }                                               //> listLengthIter: (x: List[Int])Int
  
  listLengthIter(L3)                              //> res2: Int = 31
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.