Distinct power numbers
- Task
Let given all integer combinations of for 2 <= a <= 5 and 2 <= b <= 5:
Place them in numerical order, with any repeats removed.
We get the following sequence of 15 distinct terms:
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
Ring
<lang ring> load "stdlib.ring"
see "working..." + nl see "Distinct powers are:" + nl row = 0 distPow = []
for n = 2 to 5
for m = 2 to 5 sum = pow(n,m) add(distPow,sum) next
next
distPow = sort(distPow)
for n = len(distPow) to 2 step -1
if distPow[n] = distPow[n-1] del(distPow,n-1) ok
next
for n = 1 to len(distPow)
row++ see "" + distPow[n] + " " if row%5 = 0 see nl ok
next
see "Found " + row + " numbers" + nl see "done..." + nl </lang>
- Output:
working... Distinct powers are: 4 8 9 16 25 27 32 64 81 125 243 256 625 1024 3125 Found 15 numbers done...
Wren
<lang ecmascript>import "/seq" for Lst import "/fmt" for Fmt
var pows = [] for (a in 2..5) {
var pow = a for (b in 2..5) { pow = pow * a pows.add(pow) }
} pows = Lst.distinct(pows).sort() System.print("Ordered distinct values of a ^ b for a in [2..5] and b in [2..5]:") for (chunk in Lst.chunks(pows, 5)) Fmt.print("$,5d", chunk) System.print("\nFound %(pows.count) such numbers.")</lang>
- Output:
Ordered distinct values of a ^ b for a in [2..5] and b in [2..5]: 4 8 9 16 25 27 32 64 81 125 243 256 625 1,024 3,125 Found 15 such numbers.