-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from andreas-roehler/master
solution2.5.2.3.scala
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
Exercise 2.5.2.3 | ||
For a given integer 𝑛, compute the sum of cubed digits, then the sum | ||
of cubed digits of the result, etc.; | ||
stop the resulting sequence when it repeats itself, and so determine | ||
whether it ever reaches 1. | ||
(Use Exercise 2.5.2.1.) | ||
def cubes(n: Int): Stream[Int] = ??? | ||
def cubesReach1(n: Int): Boolean = ??? | ||
scala> cubesReach1(10) | ||
res3: Boolean = true | ||
scala> cubesReach1(4) | ||
res4: Boolean = false | ||
*/ | ||
|
||
def digitsOf(n: Int): Seq[Int] = { | ||
Stream.iterate(n) { nk => nk / 10 } | ||
.takeWhile { nk => nk != 0 } | ||
.map { nk => nk % 10 } | ||
.toList.reverse | ||
} | ||
|
||
def digitsListSum(x: Int, xs: Seq[Int] = Seq(), f: (Int => Int)): Seq[Int] = { | ||
val a: Int = digitsOf(x).map{ x => f(x)}.sum | ||
if (xs.contains(a)) | ||
if (a == 1) xs | ||
else xs.reverse | ||
else digitsListSum(a, a +: xs, f) | ||
} | ||
|
||
def cubes(n: Int): Seq[Int] = { | ||
digitsListSum(n, xs = Seq(), { k => k * k * k }) | ||
} | ||
|
||
def cubesReach1(x: Int): Boolean = { | ||
val a = cubes(x) | ||
a(0) == 1 | ||
} | ||
|
||
|
||
cubes(123) | ||
cubes(10) | ||
cubes(4) | ||
|
||
val result = cubesReach1(10) | ||
val expected = true | ||
assert(result == expected) | ||
|
||
val result2 = cubesReach1(4) | ||
val expected2 = false | ||
assert(result2 == expected2) | ||
|
||
assert ( cubesReach1(20) == false ) | ||
assert ( cubesReach1(50) == false ) | ||
assert ( cubesReach1(100) == true ) | ||
|
||
assert ( cubesReach1(112) == true ) | ||
assert ( cubesReach1(114) == false ) | ||
assert ( cubesReach1(778) == true ) |