Skip to content

Instantly share code, notes, and snippets.

View pokutuna's full-sized avatar
👁️
👄👁👂

pokutuna pokutuna

👁️
👄👁👂
View GitHub Profile
def isPrime(n: Int): Boolean = (2 to math.sqrt(n).toInt).forall(n % _ != 0)
val groupedPrimes= Stream.from(2).filter(isPrime).dropWhile(_ < 1000).takeWhile(_ < 10000).toList.groupBy(_.toString.sortWith(_ < _))
def findArithmeticProgression(nums: List[Int]): Option[List[Int]] = {
@annotation.tailrec
def rec(h: Int, tail: List[Int]): Option[List[Int]] = {
if (tail.length < 2) return None
tail.find( n => tail.contains(n + (n - h)) ) match {
case Some(n) => return Some(List(h, n, n + (n - h)))
val result = 1 to 1000 map(BigInt(_)) reduceLeft( (sum, n) => sum + n.pow(n.toInt) )
println(result.toString.takeRight(10)) // => 9110846700
def isPrime(n: Int): Boolean = (2 to math.sqrt(n).toInt).forall(n % _ != 0)
lazy val primes: Stream[Int] = Stream.from(2).filter(isPrime)
def primeFactors(num: Int): Seq[Int] = {
def rec(num: Int, factors: List[Int]): List[Int] = {
if (num <= 1) return factors
val f = primes.find(num % _ == 0).get
rec(num / f, f :: factors)
}
import math._
import Stream._
def isPrime(n: Int): Boolean = (2 to sqrt(n).toInt).forall(n % _ != 0)
lazy val doubleSquares: Stream[Int] = from(1).map(pow(_, 2).toInt * 2)
lazy val primes: Stream[Int] = from(2).filter(isPrime)
def isSumOfDoubleSquareAndPrime(num: Int): Boolean = {
lazy val hexangularNumber: Stream[Int] = {
def n(index: Int = 1): Stream[Int] = (index * (2 * index - 1)) #:: n(index + 1)
n(1)
}
def isPentagonal(num: Int): Boolean = {
val n = (1 + scala.math.sqrt(1 + 24 * num.toLong)) / 6.0
return n == n.toInt
}
lazy val pentagonalNumber: Stream[Int] = {
def gen(index: Int = 1): Stream[Int] = (index * (3 * index - 1) / 2) #:: gen(index + 1)
gen(1)
}
def isPentagonal(num: Int): Boolean = {
val n = (1 + scala.math.sqrt(1 + 24 * num)) / 6.0
return n == n.toInt
}
val pandigitals = (0 to 9 permutations).map(_.mkString)
val dividers = List(1, 2, 3, 5, 7, 11, 13, 17)
def divisibleAllPartition(num: String): Boolean =
num.sliding(3, 1).zip(dividers.iterator).forall( p => p._1.toInt % p._2 == 0)
val result = pandigitals.filter(divisibleAllPartition).map(BigInt(_)).sum
println(result) // => 16695334890
*.elc
val alphabetToNum = " ABCDEFGHIJKLMNOPQRSTUVWXYZ".zipWithIndex.toMap
def getNumberOfWord(word: String): Int = word.map(alphabetToNum.getOrElse(_, 0)).sum
lazy val triangularNumbers: Stream[Int] =
1 #:: 3 #:: triangularNumbers.zipWithIndex.tail.map(n => (n._2 + 2) * (n._2 + 3) / 2)
def isTriangularWord(word: String): Boolean =
triangularNumbers.takeWhile(_ <= getNumberOfWord(word)).last == getNumberOfWord(word)
/*
var CookieUtil = CookieUtil || {};
CookieUtil.setCookie = function(params) {
for (var key in params) {
var c = encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '; path=/';
document.cookie = c;
}
};