Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Happy-numbers/Scala/happy-numbers.scala
Normal file
24
Task/Happy-numbers/Scala/happy-numbers.scala
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
scala> def isHappy(n: Int) = {
|
||||
| new Iterator[Int] {
|
||||
| val seen = scala.collection.mutable.Set[Int]()
|
||||
| var curr = n
|
||||
| def next = {
|
||||
| val res = curr
|
||||
| curr = res.toString.map(_.asDigit).map(n => n * n).sum
|
||||
| seen += res
|
||||
| res
|
||||
| }
|
||||
| def hasNext = !seen.contains(curr)
|
||||
| }.toList.last == 1
|
||||
| }
|
||||
isHappy: (n: Int)Boolean
|
||||
|
||||
scala> Iterator from 1 filter isHappy take 8 foreach println
|
||||
1
|
||||
7
|
||||
10
|
||||
13
|
||||
19
|
||||
23
|
||||
28
|
||||
31
|
||||
Loading…
Add table
Add a link
Reference in a new issue