Numbers with prime digits whose sum is 13: Difference between revisions
m ('vhich' (sic) -> whose) |
mNo edit summary |
||
Line 1: | Line 1: | ||
{{draft task}} |
{{draft task}} |
||
Find all the numbers whose digits are primes sum to 13. |
Find all the numbers whose digits are all primes and sum to 13. |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
Revision as of 15:45, 29 September 2020
Find all the numbers whose digits are all primes and sum to 13.
Factor
<lang factor>USING: formatting io kernel math math.combinatorics math.functions math.ranges sequences sequences.extras ;
- digits>number ( seq -- n ) reverse 0 [ 10^ * + ] reduce-index ;
"Numbers whose digits are prime and sum to 13:" print { 2 3 5 7 } 3 6 [a,b] [ selections [ sum 13 = ] filter ] with map-concat [ digits>number ] map "%[%d, %]\n" printf</lang>
- Output:
Numbers whose digits are prime and sum to 13: { 337, 355, 373, 535, 553, 733, 2227, 2272, 2335, 2353, 2533, 2722, 3235, 3253, 3325, 3352, 3523, 3532, 5233, 5323, 5332, 7222, 22225, 22252, 22333, 22522, 23233, 23323, 23332, 25222, 32233, 32323, 32332, 33223, 33232, 33322, 52222, 222223, 222232, 222322, 223222, 232222, 322222 }
Go
Reuses code from some other tasks. <lang go>package main
import (
"fmt" "sort" "strconv"
)
func combrep(n int, lst []byte) [][]byte {
if n == 0 { return [][]byte{nil} } if len(lst) == 0 { return nil } r := combrep(n, lst[1:]) for _, x := range combrep(n-1, lst) { r = append(r, append(x, lst[0])) } return r
}
func shouldSwap(s []byte, start, curr int) bool {
for i := start; i < curr; i++ { if s[i] == s[curr] { return false } } return true
}
func findPerms(s []byte, index, n int, res *[]string) {
if index >= n { *res = append(*res, string(s)) return } for i := index; i < n; i++ { check := shouldSwap(s, index, i) if check { s[index], s[i] = s[i], s[index] findPerms(s, index+1, n, res) s[index], s[i] = s[i], s[index] } }
}
func main() {
primes := []byte{2, 3, 5, 7} var res []string for n := 3; n <= 6; n++ { reps := combrep(n, primes) for _, rep := range reps { sum := byte(0) for _, r := range rep { sum += r } if sum == 13 { var perms []string for i := 0; i < len(rep); i++ { rep[i] += 48 } findPerms(rep, 0, len(rep), &perms) res = append(res, perms...) } } } res2 := make([]int, len(res)) for i, r := range res { res2[i], _ = strconv.Atoi(r) } sort.Ints(res2) fmt.Println("Those numbers whose digits are all prime and sum to 13 are:") fmt.Println(res2)
}</lang>
- Output:
Those numbers whose digits are all prime and sum to 13 are: [337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222]
Raku
<lang perl6>put join ', ', sort +*, unique flat
< 2 2 2 2 2 3 3 3 5 5 7 >.combinations .grep( *.sum == 13 ) .map( { .join => $_ } ) .map: { .value.permutations».join }</lang>
- Output:
337, 355, 373, 535, 553, 733, 2227, 2272, 2335, 2353, 2533, 2722, 3235, 3253, 3325, 3352, 3523, 3532, 5233, 5323, 5332, 7222, 22225, 22252, 22333, 22522, 23233, 23323, 23332, 25222, 32233, 32323, 32332, 33223, 33232, 33322, 52222, 222223, 222232, 222322, 223222, 232222, 322222
Ring
<lang ring> load "stdlib.ring"
sum = 0 limit = 1000000 aPrimes = []
for n = 1 to limit
sum = 0 st = string(n) for m = 1 to len(st) num = number(st[m]) if isprime(num) sum = sum + num flag = 1 else flag = 0 exit ok next if flag = 1 and sum = 13 add(aPrimes,n) ok
next
see "Unlucky numbers are:" + nl see showArray(aPrimes)
func showarray vect
svect = "" for n in vect svect += "" + n + "," next ? "[" + left(svect, len(svect) - 1) + "]"
</lang>
- Output:
Unlucky numbers are: [337,355,373,535,553,733,2227,2272,2335,2353,2533,2722,3235,3253,3325,3352,3523,3532,5233,5323,5332,7222,22225,22252,22333,22522,23233,23323,23332,25222,32233,32323,32332,33223,33232,33322,52222,222223,222232,222322,223222,232222,322222]
Wren
As the only digits which are prime are [2, 3, 5, 7], it is clear that a number must have between 3 and 6 digits for them to sum to 13. <lang ecmascript>import "/math" for Nums import "/seq" for Lst import "/sort" for Sort
var combrep // recursive combrep = Fn.new { |n, lst|
if (n == 0 ) return [[]] if (lst.count == 0) return [] System.write("") // guard against VM recursion bug var r = combrep.call(n, lst[1..-1]) for (x in combrep.call(n-1, lst)) { var y = x.toList y.add(lst[0]) r.add(y) } return r
}
var permute // recursive permute = Fn.new { |input|
if (input.count == 1) return [input] var perms = [] var toInsert = input[0] System.write("") // guard against VM recursion bug for (perm in permute.call(input[1..-1])) { for (i in 0..perm.count) { var newPerm = perm.toList newPerm.insert(i, toInsert) perms.add(newPerm) } } return perms
}
var primes = [2, 3, 5, 7] var res = [] for (n in 3..6) {
var reps = combrep.call(n, primes) for (rep in reps) { if (Nums.sum(rep) == 13) { var perms = permute.call(rep) for (i in 0...perms.count) perms[i] = Num.fromString(perms[i].join()) res.addAll(Lst.distinct(perms)) } }
} Sort.quick(res) System.print("Those numbers whose digits are all prime and sum to 13 are:") System.print(res)</lang>
- Output:
Those numbers whose digits are all prime and sum to 13 are: [337, 355, 373, 535, 553, 733, 2227, 2272, 2335, 2353, 2533, 2722, 3235, 3253, 3325, 3352, 3523, 3532, 5233, 5323, 5332, 7222, 22225, 22252, 22333, 22522, 23233, 23323, 23332, 25222, 32233, 32323, 32332, 33223, 33232, 33322, 52222, 222223, 222232, 222322, 223222, 232222, 322222]