Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Smith-numbers/Wren/smith-numbers.wren
Normal file
24
Task/Smith-numbers/Wren/smith-numbers.wren
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import "/math" for Int
|
||||
import "/fmt" for Fmt
|
||||
import "/seq" for Lst
|
||||
|
||||
var sumDigits = Fn.new { |n|
|
||||
var sum = 0
|
||||
while (n > 0) {
|
||||
sum = sum + n%10
|
||||
n = (n/10).floor
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
var smiths = []
|
||||
System.print("The Smith numbers below 10,000 are:")
|
||||
for (i in 2...10000) {
|
||||
if (!Int.isPrime(i)) {
|
||||
var thisSum = sumDigits.call(i)
|
||||
var factors = Int.primeFactors(i)
|
||||
var factSum = factors.reduce(0) { |acc, f| acc + sumDigits.call(f) }
|
||||
if (thisSum == factSum) smiths.add(i)
|
||||
}
|
||||
}
|
||||
for (chunk in Lst.chunks(smiths, 16)) Fmt.print("$4d", chunk)
|
||||
Loading…
Add table
Add a link
Reference in a new issue