Distinct power numbers: Difference between revisions
(Added Go) |
m (→{{header|REXX}}: simplified the code.) |
||
Line 59: | Line 59: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX pgm finds and displays distinct power integers: a^b, where a and b are 2≤both≤5*/ |
<lang rexx>/*REXX pgm finds and displays distinct power integers: a^b, where a and b are 2≤both≤5*/ |
||
parse arg |
parse arg lo hi cols . /*obtain optional arguments from the CL*/ |
||
if |
if lo=='' | lo=="," then lo= 2 /*Not specified? Then use the default.*/ |
||
if lo=='' | lo=="," then lo= 2 /* " " " " " " */ |
|||
if hi=='' | hi=="," then hi= 5 /* " " " " " " */ |
if hi=='' | hi=="," then hi= 5 /* " " " " " " */ |
||
if cols=='' | cols=="," then cols= 10 /* " " " " " " */ |
if cols=='' | cols=="," then cols= 10 /* " " " " " " */ |
||
Line 77: | Line 76: | ||
found= 0; idx= 1 /*initialize # distinct power integers.*/ |
found= 0; idx= 1 /*initialize # distinct power integers.*/ |
||
$= /*a list of distinct power integers. */ |
$= /*a list of distinct power integers. */ |
||
do j=0 for mx+1 |
do j=0 for mx+1; if @.j==. then iterate /*Number not found in 1st DO loop? Skip*/ |
||
if @.j==. then iterate /*this number "found" in 1st DO loop?*/ |
|||
found= found + 1; c= commas(j) /*maybe add commas to the number. */ |
found= found + 1; c= commas(j) /*maybe add commas to the number. */ |
||
$= $ right(c, max(w, length(c) ) ) /*add a distinct power number ──► list.*/ |
$= $ right(c, max(w, length(c) ) ) /*add a distinct power number ──► list.*/ |
Revision as of 09:01, 16 August 2021
- 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
Go
<lang go>package main
import (
"fmt" "rcu" "sort"
)
func main() {
var pows []int for a := 2; a <= 5; a++ { pow := a for b := 2; b <= 5; b++ { pow *= a pows = append(pows, pow) } } set := make(map[int]bool) for _, e := range pows { set[e] = true } pows = pows[:0] for k := range set { pows = append(pows, k) } sort.Ints(pows) fmt.Println("Ordered distinct values of a ^ b for a in [2..5] and b in [2..5]:") for i, pow := range pows { fmt.Printf("%5s ", rcu.Commatize(pow)) if (i+1)%5 == 0 { fmt.Println() } } fmt.Println("\nFound", len(pows), "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.
REXX
<lang rexx>/*REXX pgm finds and displays distinct power integers: a^b, where a and b are 2≤both≤5*/ parse arg lo hi cols . /*obtain optional arguments from the CL*/ if lo== | lo=="," then lo= 2 /*Not specified? Then use the default.*/ if hi== | hi=="," then hi= 5 /* " " " " " " */ if cols== | cols=="," then cols= 10 /* " " " " " " */ w= 10 /*width of a number in any column. */ title= ' distinct power integers, a^b, where a and b are: ' lo "≤ both ≤" hi say ' index │'center(title, 1 + cols*(w+1) ) say '───────┼'center("" , 1 + cols*(w+1), '─') @.= .; mx= 0 /*the default value for the @. array.*/
do a=lo to hi /*traipse through A values (LO──►HI).*/ do b=lo to hi /* " " B " " " */ x= a**b; if @.x\==. then iterate /*Has it been found before? Then skip.*/ @.x= x; mx= max(mx, x) /*assign the power product; fix the max*/ end /*b*/ end /*a*/
found= 0; idx= 1 /*initialize # distinct power integers.*/ $= /*a list of distinct power integers. */
do j=0 for mx+1; if @.j==. then iterate /*Number not found in 1st DO loop? Skip*/ found= found + 1; c= commas(j) /*maybe add commas to the number. */ $= $ right(c, max(w, length(c) ) ) /*add a distinct power number ──► list.*/ if found//cols\==0 then iterate /*have we populated a line of output? */ say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ idx= idx + cols /*bump the index count for the output*/ end /*j*/
if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say '───────┴'center("" , 1 + cols*(w+1), '─') say say 'Found ' commas(found) title exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</lang>
- output when using the default inputs:
index │ distinct power integers, a^b, where a and b are: 2 ≤ both ≤ 5 ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 4 8 9 16 25 27 32 64 81 125 11 │ 243 256 625 1,024 3,125 ───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────── Found 15 distinct power integers, a^b, where a and b are: 2 ≤ both ≤ 5
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.