Rebol3 Code Examplex
Achilles numbers
Positive integers that are powerful but not perfect powers.
Rebol [
title: "Rosetta code: Achilles numbers"
file: %Achilles_numbers.r3
url: https://rosettacode.org/wiki/Achilles_numbers
]
phi: function [
{Euler's totient function}
n [integer!]
][
result: n p: 2
while [p * p <= n] [
if zero? n % p [
while [zero? n % p] [n: n // p]
result: result - (result // p)
]
++ p
]
if n > 1 [result: result - (result // n)]
result
]
powerful?: function [
{Check if n is a powerful number (all prime factors appear at least squared)}
n [integer!]
][
m: n p: 2
while [p * p <= n] [
if zero? m % p [
exp: 0
while [zero? m % p] [
m: m // p
++ exp
]
if exp < 2 [return false]
]
++ p
]
m = 1
]
perfect-power?: function [
{Check if n is a perfect power (n = r^k for some integers r,k >= 2)}
n [integer!]
][
k: 2
while [k <= to integer! (log-2 n)] [
root: n ** (1.0 / k)
r: round root
if (to integer! r ** k) = n [return true]
++ k
]
false
]
achilles?: func [
{Check if n is an Achilles number (powerful but not a perfect power)}
n [integer!]
][
all [powerful? n not perfect-power? n]
]
achilles: make block! 50
strong: make block! 50
digit-counts: #(uint32! [0 0 0 0 0 0 0])
for n 2 1000000 1 [
if achilles? n [
d: 1 + to integer! log-10 n
digit-counts/:d: 1 + digit-counts/:d
if 50 > length? achilles [
append achilles n
]
if all [50 > length? strong achilles? phi n] [
append strong n
]
]
]
print "First 50 Achilles numbers:"
probe new-line/skip achilles true 10
print ""
print "First 50 Strong Achilles numbers:"
probe new-line/skip strong true 10
print ""
for d 2 6 1 [
print [
"Achilles numbers with" d "digits:" digit-counts/:d
]
]Output:
First 50 Achilles numbers:
[
72 108 200 288 392 432 500 648 675 800
864 968 972 1125 1152 1323 1352 1372 1568 1800
1944 2000 2312 2592 2700 2888 3087 3200 3267 3456
3528 3872 3888 4000 4232 4500 4563 4608 5000 5292
5324 5400 5408 5488 6075 6125 6272 6728 6912 7200
]
First 50 Strong Achilles numbers:
[
500 864 1944 2000 2592 3456 5000 10125 10368 12348
12500 16875 19652 19773 30375 31104 32000 33275 37044 40500
49392 50000 52488 55296 61731 64827 67500 69984 78608 80000
81000 83349 84375 93312 108000 111132 124416 128000 135000 148176
151875 158184 162000 165888 172872 177957 197568 200000 202612 209952
]
Achilles numbers with 2 digits: 1
Achilles numbers with 3 digits: 12
Achilles numbers with 4 digits: 47
Achilles numbers with 5 digits: 192
Achilles numbers with 6 digits: 664