-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectEuler62.scala
More file actions
33 lines (28 loc) · 1006 Bytes
/
ProjectEuler62.scala
File metadata and controls
33 lines (28 loc) · 1006 Bytes
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
31
32
33
/ **
* cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053).
* In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.
* Find the smallest cube for which exactly five permutations of its digits are cube.
*/
import collection.mutable.Map
import collection.mutable.ArrayBuffer
object WhisperCodingTest {
def findMinCub(number : Long, permuteCount :Int): Long ={
var n :Long = number
val cubeMap = Map[collection.immutable.Map[Char,Int], ArrayBuffer[Long]]()
while(n < Long.MaxValue){
var cube :Long = n*n*n
val smap = cube.toString.groupBy(_.toChar).map{ p => (p._1, p._2.length)}
if(cubeMap.contains(smap)){
if (cubeMap(smap).size+1 == permuteCount)
return cubeMap(smap).min
else
cubeMap(smap).append(n)
}else{
cubeMap(smap) = ArrayBuffer[Long](n)
}
n+=1
}
//throw exception
return -1
}
}