-
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.
solution2.5.2.9.scala provided (#77)
Signed-off-by: Andreas Roehler <andreas.roehler@online.de>
- Loading branch information
1 parent
0ea97f1
commit dd95ddb
Showing
1 changed file
with
59 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,59 @@ | ||
/** | ||
Exercise 2.5.2.9 | ||
Define a function findPalindrome: Long => Long | ||
performing the following computation: | ||
First define f(n) = revDigits(n) + n for a given integer n, | ||
where the function revDigits was defined in Exercise 2.5.2.8. | ||
If f(n) is a palindrome integer, findPalindrome returns that integer. | ||
Otherwise, it keeps applying the same transformation and computes | ||
f(n), f(f(n)), ..., until a palindrome integer is eventually found | ||
(this is mathematically guaranteed). | ||
A sample test: | ||
scala> findPalindrome(10101) | ||
val res0: Long = 20202 | ||
scala> findPalindrome(123) | ||
res0: Long = 444 | ||
scala> findPalindrome(83951) | ||
res1: Long = 869363968 | ||
*/ | ||
|
||
def revDigits (n: Long): Long = { | ||
n.toString.reverse.toLong | ||
} | ||
|
||
def findPalindrome(n: Long): Long = { | ||
val a = n.toString.reverse.toLong + n | ||
if (revDigits(a) == a) a | ||
else | ||
findPalindrome(a) | ||
} | ||
|
||
val result = findPalindrome(10101) | ||
val expected: Long = 20202 | ||
assert(result == expected) | ||
|
||
val result1 = findPalindrome(123) | ||
val expected1: Long = 444 | ||
assert(result1 == expected1) | ||
|
||
val result2 = findPalindrome(83951) | ||
val expected2: Long = 869363968 | ||
assert(result2 == expected2) | ||
|
||
// scala> :load solution2.5.2.9.scala | ||
// :load solution2.5.2.9.scala | ||
// def revDigits(n: Long): Long | ||
// def findPalindrome(n: Long): Long | ||
// val result: Long = 20202 | ||
// val expected: Long = 20202 | ||
// val result1: Long = 444 | ||
// val expected1: Long = 444 | ||
// val result2: Long = 869363968 | ||
// val expected2: Long = 869363968 |