Kendall tau distance in Scala - scala

Is this a correct implementation of Kendall tau distance in Scala
def distance[A : Ordering](s: Seq[A], t: Seq[A]): Int = {
assert(s.size == t.size, "Both sequences should be of the same length")
s.combinations(2).zip(t.combinations(2)).count {
case (Seq(s1, s2), Seq(t1, t2)) =>
(s1 > s2 && t1 < t2) || (s1 < s2 && t1 > t2)
}
}
The problem is I do not have enough data to test the algorithm on, only a few examples from Wikipedia. And I do not understand the algorithm well enough to generate my own test data. Most sources are about Kendall tau rank correlation coefficient, which is related but different animal. Maybe I could somehow derive one from the other?
For now let's say that performance is not important.
UPDATE
So, now I have three implementations of Kendall tau distance algorithm. Two of them (distance1 and distance3) give identical results (see bellow). So, which one is correct?
import scala.math.Ordering.Implicits._
val permutations = Random.shuffle((0 until 5).permutations).take(100)
println("s\tt\tDist1\tDist2\tDist3")
permutations.sliding(2).foreach { case Seq(s, t) =>
println(s.mkString(",")+"\t"+t.mkString(",")+"\t"+distance1(s, t)+"\t"+distance2(s, t)+
"\t"+distance3(s, t))
}
def distance1[A : Ordering](s: Seq[A], t: Seq[A]): Int = {
assert(s.size == t.size, "Both sequences should be of the same length")
s.combinations(2).zip(t.combinations(2)).count { case (Seq(s1, s2), Seq(t1, t2)) =>
(s1 > s2 && t1 < t2) || (s1 < s2 && t1 > t2)
}
}
def distance2[A](a: Seq[A], b: Seq[A]): Int = {
val aMap = a.zipWithIndex.toMap // map of a items to their ranks
val bMap = b.zipWithIndex.toMap // map of b items to their ranks
a.combinations(2).count{case Seq(i, j) =>
val a1 = aMap.get(i).get // rank of i in A
val a2 = aMap.get(j).get // rank of j in A
val b1 = bMap.get(i).get // rank of i in B
val b2 = bMap.get(j).get // rank of j in B
a1.compare(a2) != b1.compare(b2)
}
}
def distance3(τ_1: Seq[Int], τ_2: Seq[Int]) =
(0 until τ_1.size).map { i =>
(i+1 until τ_2.size).count { j =>
(τ_1(i) < τ_1(j) && τ_2(i) > τ_2(j)) || (τ_1(i) > τ_1(j) && τ_2(i) < τ_2(j))
}
}.sum
And here are some results:
s t Dist1 Dist2 Dist3
3,0,4,2,1 1,4,3,0,2 6 6 6
1,4,3,0,2 0,4,1,2,3 3 5 3
0,4,1,2,3 4,0,1,3,2 8 2 8
4,0,1,3,2 1,2,0,4,3 4 6 4
1,2,0,4,3 2,3,1,4,0 3 5 3
2,3,1,4,0 1,0,3,2,4 8 6 8
1,0,3,2,4 1,3,2,4,0 7 3 7
1,3,2,4,0 4,3,0,1,2 6 6 6
4,3,0,1,2 1,0,2,4,3 7 7 7
1,0,2,4,3 3,4,1,2,0 8 8 8
3,4,1,2,0 1,4,2,0,3 5 5 5
1,4,2,0,3 1,0,3,4,2 8 4 8

I don't think this is quite right. Here's some quickly written code that emphasizes that what you are comparing is the rank of the items in the sequences (you don't really want to keep those get(n).get calls in your code though). I used compare, too, which I think makes sense:
def tauDistance[A](a: Seq[A], b: Seq[A]) = {
val aMap = a.zipWithIndex.toMap // map of a items to their ranks
val bMap = b.zipWithIndex.toMap // map of b items to their ranks
a.combinations(2).count{case Seq(i, j) =>
val a1 = aMap.get(i).get // rank of i in A
val a2 = aMap.get(j).get // rank of j in A
val b1 = bMap.get(i).get // rank of i in B
val b2 = bMap.get(j).get // rank of j in B
a1.compare(a2) != b1.compare(b2)
}
}

So, the Wikipedia defines K on the ranks of the elements like this:
K(τ_1,τ_2) = |{(i,j): i < j, (τ_1(i) < τ_1(j) && τ_2(i) > τ_2(j)) || (τ_1(i) > τ_1(j) && τ_2(i) < τ_2(j))}|
We can implement this pretty directly in Scala, remembering that the inputs are sequences of ranks, not the items themselves:
def K(τ_1: Seq[Int], τ_2: Seq[Int]) =
(0 until τ_1.size).map{i =>
(i+1 until τ_2.size).count{j =>
(τ_1(i) < τ_1(j) && τ_2(i) > τ_2(j)) || (τ_1(i) > τ_1(j) && τ_2(i) < τ_2(j))
}
}.sum
This is actually a bit preferable to the tauDistance function above, since that function assumes all the items are unique (and so will fail if the sequences have duplicates) while this one works on the ranks directly.
Working with combinatoric functions is hard sometimes, and it's often not enough just to have unit tests that pass.

Related

SCALA: Generating a list of Tuple2 objects meeting some criteria

I want to generate a list of Tuple2 objects. Each tuple (a,b) in the list should meeting the conditions:a and b both are perfect squares,(b/30)<a<b
and a>N and b>N ( N can even be a BigInt)
I am trying to write a scala function to generate the List of Tuples meeting the above requirements?
This is my attempt..it works fine for Ints and Longs..But for BigInt there is sqrt problem I am facing..Here is my approach in coding as below:
scala> def genTups(N:Long) ={
| val x = for(s<- 1L to Math.sqrt(N).toLong) yield s*s;
| val y = x.combinations(2).map{ case Vector(a,b) => (a,b)}.toList
| y.filter(t=> (t._1*30/t._2)>=1)
| }
genTups: (N: Long)List[(Long, Long)]
scala> genTups(30)
res32: List[(Long, Long)] = List((1,4), (1,9), (1,16), (1,25), (4,9), (4,16), (4,25), (9,16), (9,25), (16,25))
Improved this using BigInt square-root algorithm as below:
def genTups(N1:BigInt,N2:BigInt) ={
def sqt(n:BigInt):BigInt = {
var a = BigInt(1)
var b = (n>>5)+BigInt(8)
while((b-a) >= 0) {
var mid:BigInt = (a+b)>>1
if(mid*mid-n> 0) b = mid-1
else a = mid+1
}; a-1 }
val x = for(s<- sqt(N1) to sqt(N2)) yield s*s;
val y = x.combinations(2).map{ case Vector(a,b) => (a,b)}.toList
y.filter(t=> (t._1*30/t._2)>=1)
}
I appreciate any help to improve in my algorithm .
You can avoid sqrt in you algorithm by changing the way you calculate x to this:
val x = (BigInt(1) to N).map(x => x*x).takeWhile(_ <= N)
The final function is then:
def genTups(N: BigInt) = {
val x = (BigInt(1) to N).map(x => x*x).takeWhile(_ <= N)
val y = x.combinations(2).map { case Vector(a, b) if (a < b) => (a, b) }.toList
y.filter(t => (t._1 * 30 / t._2) >= 1)
}
You can also re-write this as a single chain of operations like this:
def genTups(N: BigInt) =
(BigInt(1) to N)
.map(x => x * x)
.takeWhile(_ <= N)
.combinations(2)
.map { case Vector(a, b) if a < b => (a, b) }
.filter(t => (t._1 * 30 / t._2) >= 1)
.toList
In a quest for performance, I came up with this recursive version that appears to be significantly faster
def genTups(N1: BigInt, N2: BigInt) = {
def sqt(n: BigInt): BigInt = {
var a = BigInt(1)
var b = (n >> 5) + BigInt(8)
while ((b - a) >= 0) {
var mid: BigInt = (a + b) >> 1
if (mid * mid - n > 0) {
b = mid - 1
} else {
a = mid + 1
}
}
a - 1
}
#tailrec
def loop(a: BigInt, rem: List[BigInt], res: List[(BigInt, BigInt)]): List[(BigInt, BigInt)] =
rem match {
case Nil => res
case head :: tail =>
val a30 = a * 30
val thisRes = rem.takeWhile(_ <= a30).map(b => (a, b))
loop(head, tail, thisRes.reverse ::: res)
}
val squares = (sqt(N1) to sqt(N2)).map(s => s * s).toList
loop(squares.head, squares.tail, Nil).reverse
}
Each recursion of the loop adds all the matching pairs for a given value of a. The result is built in reverse because adding to the front of a long list is much faster than adding to the tail.
Firstly create a function to check if number if perfect square or not.
def squareRootOfPerfectSquare(a: Int): Option[Int] = {
val sqrt = math.sqrt(a)
if (sqrt % 1 == 0)
Some(sqrt.toInt)
else
None
}
Then, create another func that will calculate this list of tuples according to the conditions mentioned above.
def generateTuples(n1:Int,n2:Int)={
for{
b <- 1 to n2;
a <- 1 to n1 if(b>a && squareRootOfPerfectSquare(b).isDefined && squareRootOfPerfectSquare(a).isDefined)
} yield ( (a,b) )
}
Then on calling the function with parameters generateTuples(5,10)
you will get an output as
res0: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((1,4), (1,9), (4,9))
Hope that helps !!!

Scala - Combining two sequences to consecutively increasing triples

What is a nice and efficient functional way of solving the following problem? In imperative style, this can be done in linear time.
Given two sorted sequences p and q, f returns a sequence r (or any collection) of triples where for every triple (a,b,c) in r, the following hold:
(a < b < c)
One of the following two holds:
a,c are two consecutive elements p, and b is in q
a,c are two consecutive elements q, and b is in p
Example: Consider the following two sequences.
val p = Seq(1,4,5,7,8,9)
val q = Seq(2,3,6,7,8,10)
Then f(p,s) computes the following sequence:
Seq((1,2,4), (1,3,4), (5,6,7), (3,4,6), (3,5,6), (8,9,10))
Current solution: I do not find this one very elegant. I am looking for a better one.
def consecutiveTriplesOneWay(s1: Seq[Int], s2:Seq[Int]) = {
for {
i <- 0 until s1.size - 1 if s1(i) < s1(i+1)
j <- 0 until s2.size if s1(i) < s2(j) && s2(j) < s1(i+1)
} yield (s1(i), s2(j), s1(i+1))
}
def consecutiveTriples(s1: Seq[Int], s2:Seq[Int]) =
consecutiveTriplesOneWay(s1, s2) ++ consecutiveTriplesOneWay(s2, s1)
def main(args: Array[String]) {
val p = Seq(1,4,5,7,8,9)
val q = Seq(2,3,6,7,8,10)
consecutiveTriples(p, q).foreach(println(_))
}
Edit: My imperative solution
def consecutiveTriplesOneWayImperative(s1: Seq[Int], s2:Seq[Int]) = {
var i = 0
var j = 0
val triples = mutable.MutableList.empty[(Int,Int,Int)]
while (i < s1.size - 1 && j < s2.size) {
if (s1(i) < s2(j) && s2(j) < s1(i + 1)) {
triples += ((s1(i), s2(j), s1(i + 1)))
j += 1
} else if (s1(i) >= s2(j))
j += 1
else
i += 1
}
triples.toSeq
}
def consecutiveTriples(s1: Seq[Int], s2:Seq[Int]) =
consecutiveTriplesOneWayImperative(s1,s2) ++
consecutiveTriplesOneWayImperative(s2,s1)
Imperative solution translated to tailrec. Bit verbose but works
def consecutiveTriplesRec(s1: Seq[Int], s2: Seq[Int]) = {
#tailrec
def consTriplesOneWay(left: Seq[Int], right: Seq[Int],
triples: Seq[(Int, Int, Int)]): Seq[(Int, Int, Int)] = {
(left, right) match {
case (l1 :: l2 :: ls, r :: rs) =>
if (l1 < r && r < l2) consTriplesOneWay(left, rs, (l1, r, l2) +: triples)
else if (l1 >= r) consTriplesOneWay(left, rs, triples)
else consTriplesOneWay(l2 :: ls, right, triples)
case _ => triples
}
}
consTriplesOneWay(s1, s2, Nil) ++ consTriplesOneWay(s2, s1, Nil)
}

Scala - can 'for-yield' clause yields nothing for some condition?

In Scala language, I want to write a function that yields odd numbers within a given range. The function prints some log when iterating even numbers. The first version of the function is:
def getOdds(N: Int): Traversable[Int] = {
val list = new mutable.MutableList[Int]
for (n <- 0 until N) {
if (n % 2 == 1) {
list += n
} else {
println("skip even number " + n)
}
}
return list
}
If I omit printing logs, the implementation become very simple:
def getOddsWithoutPrint(N: Int) =
for (n <- 0 until N if (n % 2 == 1)) yield n
However, I don't want to miss the logging part. How do I rewrite the first version more compactly? It would be great if it can be rewritten similar to this:
def IWantToDoSomethingSimilar(N: Int) =
for (n <- 0 until N) if (n % 2 == 1) yield n else println("skip even number " + n)
def IWantToDoSomethingSimilar(N: Int) =
for {
n <- 0 until N
if n % 2 != 0 || { println("skip even number " + n); false }
} yield n
Using filter instead of a for expression would be slightly simpler though.
I you want to keep the sequentiality of your traitement (processing odds and evens in order, not separately), you can use something like that (edited) :
def IWantToDoSomethingSimilar(N: Int) =
(for (n <- (0 until N)) yield {
if (n % 2 == 1) {
Option(n)
} else {
println("skip even number " + n)
None
}
// Flatten transforms the Seq[Option[Int]] into Seq[Int]
}).flatten
EDIT, following the same concept, a shorter solution :
def IWantToDoSomethingSimilar(N: Int) =
(0 until N) map {
case n if n % 2 == 0 => println("skip even number "+ n)
case n => n
} collect {case i:Int => i}
If you will to dig into a functional approach, something like the following is a good point to start.
First some common definitions:
// use scalaz 7
import scalaz._, Scalaz._
// transforms a function returning either E or B into a
// function returning an optional B and optionally writing a log of type E
def logged[A, E, B, F[_]](f: A => E \/ B)(
implicit FM: Monoid[F[E]], FP: Pointed[F]): (A => Writer[F[E], Option[B]]) =
(a: A) => f(a).fold(
e => Writer(FP.point(e), None),
b => Writer(FM.zero, Some(b)))
// helper for fixing the log storage format to List
def listLogged[A, E, B](f: A => E \/ B) = logged[A, E, B, List](f)
// shorthand for a String logger with List storage
type W[+A] = Writer[List[String], A]
Now all you have to do is write your filtering function:
def keepOdd(n: Int): String \/ Int =
if (n % 2 == 1) \/.right(n) else \/.left(n + " was even")
You can try it instantly:
scala> List(5, 6) map(keepOdd)
res0: List[scalaz.\/[String,Int]] = List(\/-(5), -\/(6 was even))
Then you can use the traverse function to apply your function to a list of inputs, and collect both the logs written and the results:
scala> val x = List(5, 6).traverse[W, Option[Int]](listLogged(keepOdd))
x: W[List[Option[Int]]] = scalaz.WriterTFunctions$$anon$26#503d0400
// unwrap the results
scala> x.run
res11: (List[String], List[Option[Int]]) = (List(6 was even),List(Some(5), None))
// we may even drop the None-s from the output
scala> val (logs, results) = x.map(_.flatten).run
logs: List[String] = List(6 was even)
results: List[Int] = List(5)
I don't think this can be done easily with a for comprehension. But you could use partition.
def getOffs(N:Int) = {
val (evens, odds) = 0 until N partition { x => x % 2 == 0 }
evens foreach { x => println("skipping " + x) }
odds
}
EDIT: To avoid printing the log messages after the partitioning is done, you can change the first line of the method like this:
val (evens, odds) = (0 until N).view.partition { x => x % 2 == 0 }

Mix Scala Option and regular variable in a statement

I would like to write conditional statements mixing transparently Scala Options and regular variables.
For example:
var o1 = Some(1)
var o2: Option[Int] = None
var x = 2
val test1 = x < 3 && o1<5 //=> should be true or Some(true)
val test2 = x < 3 && o2<5 //=> should be false or None
val test3 = x < 3 || o2<5 //=> should be true (o2 not evaluated)
of course I could write
test1 = x < 3 && o1.exists (_<5)
but I would prefer a cleaner syntax.
Any hint? Should I expand 'Option' with operators, or use implicits, or category theory or else?
What about mapping from Option[Int] to Option[Boolean]?
x < 3 && (o1 map {_ < 5} getOrElse false)
x < 3 && (o2 map {_ < 5} getOrElse false)
x < 3 || (o2 map {_ < 5} getOrElse false)
Using implicits is certainly easy:
implicit def enrichOptionInt(self: Option[Int]) = new {
def <(i: Int) = self.exists(_ < i)
}
val test1 = x < 3 && o1 < 5 // True
Or if you want it to work for any kind of Numeric:
class EnrichedOptionNumeric[N: Numeric](self: Option[N]) {
def <(n: N) = self.exists(v => implicitly[Numeric[N]].lt(v, n))
}
implicit def enrichOptionNumeric[N: Numeric](self: Option[N]) = new EnrichedOptionNumeric(self)
val oD = Some(2.0)
val test1 = x < 3 && o1 < 5 // true
val testD = x < 3 && oD < 5.0 // true
EDIT to answer question in comment:
If you want to support equality, you, unfortunately, cannot use == since that operator is already defined for Option. If a method (or operator) is already defined for a class, then the implicit will never be triggered since Scala only looks for implicits when it doesn't recognize the method being called.
You can however, simply define a different symbol to mean "option equals". For example, you could use ===. To do so, you'd just add the following line to the definition of EnrichedOptionNumeric above:
def ===(n: N) = self.exists(v => implicitly[Numeric[N]].equiv(v, n))
Then you can do:
val testE = x < 3 && o1 === 1 // true
Think about whether there's a more natural way to express the "missing" value in the context of this problem than using an Option. Maybe to say there's no limit, a Double with a value of Infinity would work, or an Int with Integer.MAX_VALUE. Then remove the Option from the problem completely. If you can't then you can use
var o1 = Some (1)
var o2 = Option[Int] = None
var x = 2
val test1 = x < 3 && o1.getOrElse(Integer.MAX_VALUE)<5 //=> should be true or Some(true)
val test2 = x < 3 && o2.getOrElse(Integer.MAX_VALUE)<5 //=> should be false or None
val test3 = x < 3 || o2.getOrElse(Integer.MAX_VALUE)<5 //=> should be true (o2 not evaluated)
Tomasz Nurkiewicz's answer can be made more succinct with some Scalaz sugar, and by employing the Monoid instance for Boolean.
The "zero" value for the monoid is false, so o getOrElse false becomes ~o, using a unary operator ~ defined in scalaz. (Here I am using scalaz 6.0.4)
def p: Int => Boolean = _ < 3
def q: Int => Boolean = _ < 5
import scalaz._, Scalaz._
scala> val test1 = p(x) && ~(o1 map q)
test1: Boolean = true
scala> val test2 = p(x) && ~(o2 map q)
test2: Boolean = false
scala> val test3 = p(x) || ~(o2 map q)
test3: Boolean = true
Scala can actually make an Ordering for Option[A] if you have an Ordering[A] but the semantics are different (None is the smallest value) from what you wanted.
Also, for the comparison to work, both values need to be of the same type, so you need a way to lift the Ints into an Option. I added an opt method for that purpose.
This is an example with the internal ordering:
import scala.math.Ordering.Implicits.infixOrderingOps
//This allows you to use method/operator syntax on anything with an Ordering
implicit def mkOption[A](a: A) = new { def opt: Option[A] = Some(a) }
var o1 = Some(1)
var o2: Option[Int] = None
var x = 2
val test1 = x < 3 && o1<5.opt //=> true
val test2 = x < 3 && o2<5.opt //=> true
val test3 = x < 3 || o2<5.opt //=> true
None > 0.opt //=> false
None < 0.opt //=> true
To get closer to your semantics, we can define a new ordering
implicit def mkOptionOrdering[A: Ordering] = new Ordering[Option[A]] {
def compare(a: Option[A], b: Option[A]): Int = {
if (a.isEmpty || b.isEmpty) 0
else implicitly[Ordering[A]].compare(a.get, b.get)
}
}
Now your tests will do what you expected and the 2 extra tests will also be false but those semantics are rather odd, compare returns 0 for things that aren't equal.

Integer partitioning in Scala

Given n ( say 3 people ) and s ( say 100$ ), we'd like to partition s among n people.
So we need all possible n-tuples that sum to s
My Scala code below:
def weights(n:Int,s:Int):List[List[Int]] = {
List.concat( (0 to s).toList.map(List.fill(n)(_)).flatten, (0 to s).toList).
combinations(n).filter(_.sum==s).map(_.permutations.toList).toList.flatten
}
println(weights(3,100))
This works for small values of n. ( n=1, 2, 3 or 4).
Beyond n=4, it takes a very long time, practically unusable.
I'm looking for ways to rework my code using lazy evaluation/ Stream.
My requirements : Must work for n upto 10.
Warning : The problem gets really big really fast. My results from Matlab -
---For s =100, n = 1 thru 5 results are ---
n=1 :1 combinations
n=2 :101 combinations
n=3 :5151 combinations
n=4 :176851 combinations
n=5: 4598126 combinations
---
You need dynamic programming, or memoization. Same concept, anyway.
Let's say you have to divide s among n. Recursively, that's defined like this:
def permutations(s: Int, n: Int): List[List[Int]] = n match {
case 0 => Nil
case 1 => List(List(s))
case _ => (0 to s).toList flatMap (x => permutations(s - x, n - 1) map (x :: _))
}
Now, this will STILL be slow as hell, but there's a catch here... you don't need to recompute permutations(s, n) for numbers you have already computed. So you can do this instead:
val memoP = collection.mutable.Map.empty[(Int, Int), List[List[Int]]]
def permutations(s: Int, n: Int): List[List[Int]] = {
def permutationsWithHead(x: Int) = permutations(s - x, n - 1) map (x :: _)
n match {
case 0 => Nil
case 1 => List(List(s))
case _ =>
memoP getOrElseUpdate ((s, n),
(0 to s).toList flatMap permutationsWithHead)
}
}
And this can be even further improved, because it will compute every permutation. You only need to compute every combination, and then permute that without recomputing.
To compute every combination, we can change the code like this:
val memoC = collection.mutable.Map.empty[(Int, Int, Int), List[List[Int]]]
def combinations(s: Int, n: Int, min: Int = 0): List[List[Int]] = {
def combinationsWithHead(x: Int) = combinations(s - x, n - 1, x) map (x :: _)
n match {
case 0 => Nil
case 1 => List(List(s))
case _ =>
memoC getOrElseUpdate ((s, n, min),
(min to s / 2).toList flatMap combinationsWithHead)
}
}
Running combinations(100, 10) is still slow, given the sheer numbers of combinations alone. The permutations for each combination can be obtained simply calling .permutation on the combination.
Here's a quick and dirty Stream solution:
def weights(n: Int, s: Int) = (1 until s).foldLeft(Stream(Nil: List[Int])) {
(a, _) => a.flatMap(c => Stream.range(0, n - c.sum + 1).map(_ :: c))
}.map(c => (n - c.sum) :: c)
It works for n = 6 in about 15 seconds on my machine:
scala> var x = 0
scala> weights(100, 6).foreach(_ => x += 1)
scala> x
res81: Int = 96560646
As a side note: by the time you get to n = 10, there are 4,263,421,511,271 of these things. That's going to take days just to stream through.
My solution of this problem, it can computer n till 6:
object Partition {
implicit def i2p(n: Int): Partition = new Partition(n)
def main(args : Array[String]) : Unit = {
for(n <- 1 to 6) println(100.partitions(n).size)
}
}
class Partition(n: Int){
def partitions(m: Int):Iterator[List[Int]] = new Iterator[List[Int]] {
val nums = Array.ofDim[Int](m)
nums(0) = n
var hasNext = m > 0 && n > 0
override def next: List[Int] = {
if(hasNext){
val result = nums.toList
var idx = 0
while(idx < m-1 && nums(idx) == 0) idx = idx + 1
if(idx == m-1) hasNext = false
else {
nums(idx+1) = nums(idx+1) + 1
nums(0) = nums(idx) - 1
if(idx != 0) nums(idx) = 0
}
result
}
else Iterator.empty.next
}
}
}
1
101
5151
176851
4598126
96560646
However , we can just show the number of the possible n-tuples:
val pt: (Int,Int) => BigInt = {
val buf = collection.mutable.Map[(Int,Int),BigInt]()
(s,n) => buf.getOrElseUpdate((s,n),
if(n == 0 && s > 0) BigInt(0)
else if(s == 0) BigInt(1)
else (0 to s).map{k => pt(s-k,n-1)}.sum
)
}
for(n <- 1 to 20) printf("%2d :%s%n",n,pt(100,n).toString)
1 :1
2 :101
3 :5151
4 :176851
5 :4598126
6 :96560646
7 :1705904746
8 :26075972546
9 :352025629371
10 :4263421511271
11 :46897636623981
12 :473239787751081
13 :4416904685676756
14 :38393094575497956
15 :312629484400483356
16 :2396826047070372396
17 :17376988841260199871
18 :119594570260437846171
19 :784008849485092547121
20 :4910371215196105953021