Advertisement
Guest User

Untitled

a guest
Jul 11th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.26 KB | None | 0 0
  1. object Problem111 {
  2.   def main(args: Array[String]) {
  3.        println(sumOfSFunctions(10))
  4.   }
  5.  
  6.   def countRepeatedDigits(number: Int, digit: Int): Int = number.toString.count(_ == (digit + 48).toChar)
  7.  
  8.   def mFunction(primeLength: Int, digit: Int) = getMapOfPrimesWithMaxDigitsNumber(primeLength, digit)._1
  9.  
  10.   def nFunction(primeLength: Int, digit: Int): Int = getMapOfPrimesWithMaxDigitsNumber(primeLength, digit)._2.size
  11.  
  12.   def sFunction(primeLength: Int, digit: Int): BigInt = getMapOfPrimesWithMaxDigitsNumber(primeLength, digit)._2.sum
  13.  
  14.   def getMapOfPrimesWithMaxDigitsNumber(primeLength: Int, digit: Int): (Int, List[Int]) = {
  15.     getListOfPrimesWithLength(primeLength).groupBy(x => countRepeatedDigits(x, digit)).maxBy(_._1)
  16.   }
  17.  
  18.   def sumOfSFunctions(primeLength: Int): BigInt = (0 to 9).map(x => sFunction(primeLength, x)).sum
  19.  
  20.   def getListOfPrimesWithLength(length: Int): List[Int] = sieveOfEratosthenes(math.pow(10, length).toInt).filter(x => x >= math.pow(10, length - 1)).toList
  21.  
  22.   def sieveOfEratosthenes(nTo: Int) = {
  23.     val primes = collection.mutable.BitSet.empty.par ++ (2 to nTo)
  24.     for {
  25.       candidate <- 2 to Math.sqrt(nTo).toInt
  26.       if primes contains candidate
  27.     } primes --= candidate * candidate to nTo by candidate
  28.     primes
  29.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement