Distinct power numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 2: Line 2:


;Task:
;Task:
Let given all integer combinations of <math>a^b</math> for '''2 < a < 5''' and '''2 < b < 5''':
Let given all integer combinations of <math>a^b</math> for '''2 <= a <= 5''' and '''2 <= b <= 5''':
<br>
<math>2^2=4 ,2^3=8, 2^4=16, 2^5=32</math>
<br>
<math>3^2=9,3^3=27,3^4=81,3^5=243</math>
<br>
<math>4^2=16,4^3=64,4^4=256,4^5=1024</math>
<br>
<math>5^2=25,5^3=125,5^4=6256,5^5=3125</math>
<br>
<br>
Place them in numerical order, with any repeats removed.
Place them in numerical order, with any repeats removed.

Revision as of 04:25, 16 August 2021

Distinct power numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
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...