Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
42
Task/Smith-numbers/Scala/smith-numbers.scala
Normal file
42
Task/Smith-numbers/Scala/smith-numbers.scala
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
object SmithNumbers extends App {
|
||||
|
||||
def sumDigits(_n: Int): Int = {
|
||||
var n = _n
|
||||
var sum = 0
|
||||
while (n > 0) {
|
||||
sum += (n % 10)
|
||||
n /= 10
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
def primeFactors(_n: Int): List[Int] = {
|
||||
var n = _n
|
||||
val result = new collection.mutable.ListBuffer[Int]
|
||||
val i = 2
|
||||
while (n % i == 0) {
|
||||
result += i
|
||||
n /= i
|
||||
}
|
||||
var j = 3
|
||||
while (j * j <= n) {
|
||||
while (n % j == 0) {
|
||||
result += i
|
||||
n /= j
|
||||
}
|
||||
j += 2
|
||||
}
|
||||
if (n != 1) result += n
|
||||
result.toList
|
||||
}
|
||||
|
||||
for (n <- 1 until 10000) {
|
||||
val factors = primeFactors(n)
|
||||
if (factors.size > 1) {
|
||||
var sum = sumDigits(n)
|
||||
for (f <- factors) sum -= sumDigits(f)
|
||||
if (sum == 0) println(n)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue