Rhonda numbers: Difference between revisions

Added Python implementation for Rhonda Numbers task
(Added Algol 68)
(Added Python implementation for Rhonda Numbers task)
 
(5 intermediate revisions by 4 users not shown)
Line 533:
In base 36: rs 3pc 4di 6bi 8hi 9ks a9g c5i cz9 hrc 13to 14ou 1g9s 1iq9 1lw6
</pre>
 
=={{header|FreeBASIC}}==
{{trans|ALGOL 68}}
<syntaxhighlight lang="vbnet">'#include "isprime.bas"
 
Function FactorSum(n As Uinteger) As Uinteger
Dim As Uinteger result = 0
Dim As Uinteger v = Abs(n)
While v > 1 And v Mod 2 = 0
result += 2
v \= 2
Wend
For f As Uinteger = 3 To v Step 2
While v > 1 And v Mod f = 0
result += f
v \= f
Wend
Next f
Return result
End Function
 
Function DigitProduct(n As Uinteger, base_ As Uinteger) As Uinteger
If n = 0 Then Return 0
Dim As Uinteger result = 1
Dim As Uinteger v = Abs(n)
While v > 0
result *= v Mod base_
v \= base_
Wend
Return result
End Function
 
Function isRhonda(n As Uinteger, base_ As Uinteger) As Uinteger
Return base_ * FactorSum(n) = DigitProduct(n, base_)
End Function
 
Function ToBaseString(n As Uinteger, base_ As Uinteger) As String
If n = 0 Then Return "0"
Dim As Uinteger under10 = Asc("0")
Dim As Uinteger over9 = Asc("a") - 10
Dim As String result = ""
Dim As Uinteger v = Abs(n)
While v > 0
Dim As Uinteger d = v Mod base_
result = Chr(d + Iif(d < 10, under10, over9)) + result
v \= base_
Wend
Return result
End Function
 
Dim As Uinteger maxRhonda = 10, maxBase = 16
For base_ As Uinteger = 2 To maxBase
If Not isPrime(base_) Then
Print "The first "; maxRhonda; " Rhonda numbers in base "; base_; ":"
Dim As Uinteger rCount = 0
Dim As Uinteger rhonda(1 To maxRhonda)
Dim As Uinteger n = 1
While rCount < maxRhonda
If isRhonda(n, base_) Then
rCount += 1
rhonda(rCount) = n
End If
n += 1
Wend
Print " in base 10: ";
For i As Uinteger = 1 To maxRhonda
Print " "; rhonda(i);
Next i
Print
If base_ <> 10 Then
Print Using " in base ##: "; base_;
For i As Uinteger = 1 To maxRhonda
Print " "; ToBaseString(rhonda(i), base_);
Next i
Print
End If
End If
Next base_
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as ALGOL 68 entry.</pre>
 
=={{header|Go}}==
Line 1,593 ⟶ 1,675:
base 35:{8232,9476,9633,18634,30954,41905,52215,52440,56889,61992,62146,66339,98260,102180,103305}
base 36:{1000,4800,5670,8190,10998,12412,13300,15750,16821,23016,51612,52734,67744,70929,75030}</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/[sequtils, strformat, strutils]
 
type Base = 2..36
 
template isEven(n: int): bool = (n and 1) == 0
 
func isPrime(n: Natural): bool =
## Return true if "n" is prime.
if n < 2: return false
if n.isEven: return n == 2
if n mod 3 == 0: return n == 3
var d = 5
while d * d <= n:
if n mod d == 0: return false
inc d, 2
return true
 
func digitProduct(n: Positive; base: Base): int =
## Return the product of digits of "n" in given base.
var n = n.Natural
result = 1
while n != 0:
result *= n mod base
n = n div base
 
func primeFactorSum(n: Positive): int =
## Return the sum of prime factors of "n".
var n = n.Natural
while n.isEven:
inc result, 2
n = n shr 1
var d = 3
while d * d <= n:
while n mod d == 0:
inc result, d
n = n div d
inc d, 2
if n > 1: inc result, n
 
func isRhondaNumber(n: Positive; base: Base): bool =
## Return true if "n" is a Rhonda number to given base.
n.digitProduct(base) == base * n.primeFactorSum
 
const Digits = toSeq('0'..'9') & toSeq('a'..'z')
 
func toBase(n: Positive; base: Base): string =
## Return the string representation of "n" in given base.
var n = n.Natural
while true:
result.add Digits[n mod base]
n = n div base
if n == 0: break
# Reverse the digits.
for i in 1..(result.len shr 1):
swap result[i - 1], result[^i]
 
 
const N = 10
 
for base in 2..36:
if base.isPrime: continue
echo &"First {N} Rhonda numbers to base {base}:"
var rhondaList: seq[Positive]
var n = 1
var count = 0
while count < N:
if n.isRhondaNumber(base):
rhondaList.add n
inc count
inc n
echo "In base 10: ", rhondaList.join(" ")
echo &"In base {base}: ", rhondaList.mapIt(it.toBase(base)).join(" ")
echo()
</syntaxhighlight>
 
{{out}}
<pre>First 10 Rhonda numbers to base 4:
In base 10: 10206 11935 12150 16031 45030 94185 113022 114415 191149 244713
In base 4: 2133132 2322133 2331312 3322133 22333212 112333221 123211332 123323233 232222231 323233221
 
First 10 Rhonda numbers to base 6:
In base 10: 855 1029 3813 5577 7040 7304 15104 19136 35350 36992
In base 6: 3543 4433 25353 41453 52332 53452 153532 224332 431354 443132
 
First 10 Rhonda numbers to base 8:
In base 10: 1836 6318 6622 10530 14500 14739 17655 18550 25398 25956
In base 8: 3454 14256 14736 24442 34244 34623 42367 44166 61466 62544
 
First 10 Rhonda numbers to base 9:
In base 10: 15540 21054 25331 44360 44660 44733 47652 50560 54944 76857
In base 9: 23276 31783 37665 66758 67232 67323 72326 76317 83328 126376
 
First 10 Rhonda numbers to base 10:
In base 10: 1568 2835 4752 5265 5439 5664 5824 5832 8526 12985
In base 10: 1568 2835 4752 5265 5439 5664 5824 5832 8526 12985
 
First 10 Rhonda numbers to base 12:
In base 10: 560 800 3993 4425 4602 4888 7315 8296 9315 11849
In base 12: 3a8 568 2389 2689 27b6 29b4 4297 4974 5483 6a35
 
First 10 Rhonda numbers to base 14:
In base 10: 11475 18655 20565 29631 31725 45387 58404 58667 59950 63945
In base 14: 4279 6b27 76cd ab27 b7c1 1277d 173da 17547 17bc2 19437
 
First 10 Rhonda numbers to base 15:
In base 10: 2392 2472 11468 15873 17424 18126 19152 20079 24388 30758
In base 15: a97 aec 35e8 4a83 5269 5586 5a1c 5e39 735d 91a8
 
First 10 Rhonda numbers to base 16:
In base 10: 1000 1134 6776 15912 19624 20043 20355 23946 26296 29070
In base 16: 3e8 46e 1a78 3e28 4ca8 4e4b 4f83 5d8a 66b8 718e
 
First 10 Rhonda numbers to base 18:
In base 10: 1470 3000 8918 17025 19402 20650 21120 22156 26522 36549
In base 18: 49c 94c 1998 2g9f 35fg 39d4 3b36 3e6g 49f8 64e9
 
First 10 Rhonda numbers to base 20:
In base 10: 1815 11050 15295 21165 22165 30702 34510 34645 42292 44165
In base 20: 4af 17ca 1i4f 2ci5 2f85 3gf2 465a 46c5 55ec 5a85
 
First 10 Rhonda numbers to base 21:
In base 10: 1632 5390 8512 12992 15678 25038 29412 34017 39552 48895
In base 21: 3ef c4e j67 189e 1ebc 2eg6 33ec 3e2i 45e9 55i7
 
First 10 Rhonda numbers to base 22:
In base 10: 2695 4128 7865 28800 31710 37030 71875 74306 117760 117895
In base 22: 5cb 8be g5b 2fb2 2lb8 3ab4 6gb1 6lbc b16g b1cj
 
First 10 Rhonda numbers to base 24:
In base 10: 2080 2709 3976 5628 5656 7144 8296 9030 10094 17612
In base 24: 3eg 4gl 6lg 9ic 9jg c9g e9g fg6 hce 16dk
 
First 10 Rhonda numbers to base 25:
In base 10: 6764 9633 13260 22022 53382 57640 66015 69006 97014 140130
In base 25: ake fa8 l5a 1a5m 3aa7 3h5f 45ff 4aa6 655e 8o55
 
First 10 Rhonda numbers to base 26:
In base 10: 7788 9322 9374 11160 22165 27885 34905 44785 47385 49257
In base 26: bde dke dme gd6 16kd 1f6d 1pgd 2e6d 2i2d 2kmd
 
First 10 Rhonda numbers to base 27:
In base 10: 4797 11844 12078 13200 14841 17750 24320 26883 27477 46455
In base 27: 6fi g6i gf9 i2o k9i o9b 169k 19ni 1aii 29jf
 
First 10 Rhonda numbers to base 28:
In base 10: 3094 5808 5832 7462 11160 13671 27270 28194 28638 39375
In base 28: 3qe 7bc 7c8 9ee e6g hc7 16lq 17qq 18em 1m67
 
First 10 Rhonda numbers to base 30:
In base 10: 3024 3168 5115 5346 5950 6762 7750 7956 8470 9476
In base 30: 3ao 3fi 5kf 5s6 6ia 7fc 8ia 8p6 9ca afq
 
First 10 Rhonda numbers to base 32:
In base 10: 1944 3600 13520 15876 16732 16849 25410 25752 28951 47472
In base 32: 1so 3gg d6g fg4 gas geh oq2 p4o s8n 1ebg
 
First 10 Rhonda numbers to base 33:
In base 10: 756 7040 7568 13826 24930 30613 59345 63555 64372 131427
In base 33: mu 6fb 6vb cmw mtf s3m 1lgb 1pbu 1q3m 3lml
 
First 10 Rhonda numbers to base 34:
In base 10: 5661 14161 15620 16473 22185 37145 125579 134692 135405 138472
In base 34: 4uh c8h dhe e8h j6h w4h 36lh 3ehi 3f4h 3hqo
 
First 10 Rhonda numbers to base 35:
In base 10: 8232 9476 9633 18634 30954 41905 52215 52440 56889 61992
In base 35: 6p7 7pq 7u8 f7e p9e y7a 17lu 17sa 1bfe 1fl7
 
First 10 Rhonda numbers to base 36:
In base 10: 1000 4800 5670 8190 10998 12412 13300 15750 16821 23016
In base 36: rs 3pc 4di 6bi 8hi 9ks a9g c5i cz9 hrc
</pre>
 
=={{header|PARI/GP}}==
{{trans|Julia}}
<syntaxhighlight lang="PARI/GP">
isRhonda(n, b) =
{
local(mydigits, product, mysum, factors, pairProduct);
mydigits = digits(n, b);
product = vecprod(mydigits);
factors = factor(n);
mysum= 0;
for(i = 1, matsize(factors)[1],
pairProduct = factors[i, 1] * factors[i, 2];
mysum += pairProduct;
);
product == b * mysum;
}
 
displayrhondas(low, high, nshow) =
{
local(b, n, rhondas, count, basebRhondas);
for(b = low, high,
if(isprime(b), next);
n = 1; rhondas = [];
count = 0;
while(count < nshow,
if(isRhonda(n, b),
rhondas = concat(rhondas, n);
count++;
);
n++;
);
print("First " nshow " Rhondas in base " b ":");
print("In base 10: " rhondas);
basebRhondas = vector(#rhondas, i, (digits(rhondas[i], b)));
print("In base " b ": " basebRhondas);
print("\n");
);
}
 
displayrhondas(2, 16, 15);
</syntaxhighlight>
{{out}}
<pre style="height:40ex;overflow:scroll;">
First 15 Rhondas in base 4:
In base 10: [10206, 11935, 12150, 16031, 45030, 94185, 113022, 114415, 191149, 244713, 259753, 374782, 392121, 503773, 649902]
In base 4: [[2, 1, 3, 3, 1, 3, 2], [2, 3, 2, 2, 1, 3, 3], [2, 3, 3, 1, 3, 1, 2], [3, 3, 2, 2, 1, 3, 3], [2, 2, 3, 3, 3, 2, 1, 2], [1, 1, 2, 3, 3, 3, 2, 2, 1], [1, 2, 3, 2, 1, 1, 3, 3, 2], [1, 2, 3, 3, 2, 3, 2, 3, 3], [2, 3, 2, 2, 2, 2, 2, 3, 1], [3, 2, 3, 2, 3, 3, 2, 2, 1], [3, 3, 3, 1, 2, 2, 2, 2, 1], [1, 1, 2, 3, 1, 3, 3, 3, 3, 2], [1, 1, 3, 3, 2, 3, 2, 3, 2, 1], [1, 3, 2, 2, 3, 3, 3, 1, 3, 1], [2, 1, 3, 2, 2, 2, 2, 2, 3, 2]]
 
 
First 15 Rhondas in base 6:
In base 10: [855, 1029, 3813, 5577, 7040, 7304, 15104, 19136, 35350, 36992, 41031, 42009, 60368, 65536, 67821]
In base 6: [[3, 5, 4, 3], [4, 4, 3, 3], [2, 5, 3, 5, 3], [4, 1, 4, 5, 3], [5, 2, 3, 3, 2], [5, 3, 4, 5, 2], [1, 5, 3, 5, 3, 2], [2, 2, 4, 3, 3, 2], [4, 3, 1, 3, 5, 4], [4, 4, 3, 1, 3, 2], [5, 1, 3, 5, 4, 3], [5, 2, 2, 2, 5, 3], [1, 1, 4, 3, 2, 5, 2], [1, 2, 2, 3, 2, 2, 4], [1, 2, 4, 1, 5, 5, 3]]
 
 
First 15 Rhondas in base 8:
In base 10: [1836, 6318, 6622, 10530, 14500, 14739, 17655, 18550, 25398, 25956, 30562, 39215, 39325, 50875, 51429]
In base 8: [[3, 4, 5, 4], [1, 4, 2, 5, 6], [1, 4, 7, 3, 6], [2, 4, 4, 4, 2], [3, 4, 2, 4, 4], [3, 4, 6, 2, 3], [4, 2, 3, 6, 7], [4, 4, 1, 6, 6], [6, 1, 4, 6, 6], [6, 2, 5, 4, 4], [7, 3, 5, 4, 2], [1, 1, 4, 4, 5, 7], [1, 1, 4, 6, 3, 5], [1, 4, 3, 2, 7, 3], [1, 4, 4, 3, 4, 5]]
 
 
First 15 Rhondas in base 9:
In base 10: [15540, 21054, 25331, 44360, 44660, 44733, 47652, 50560, 54944, 76857, 77142, 83334, 83694, 96448, 97944]
In base 9: [[2, 3, 2, 7, 6], [3, 1, 7, 8, 3], [3, 7, 6, 6, 5], [6, 6, 7, 5, 8], [6, 7, 2, 3, 2], [6, 7, 3, 2, 3], [7, 2, 3, 2, 6], [7, 6, 3, 1, 7], [8, 3, 3, 2, 8], [1, 2, 6, 3, 7, 6], [1, 2, 6, 7, 3, 3], [1, 3, 6, 2, 7, 3], [1, 3, 6, 7, 2, 3], [1, 5, 6, 2, 6, 4], [1, 5, 8, 3, 1, 6]]
 
 
First 15 Rhondas in base 10:
In base 10: [1568, 2835, 4752, 5265, 5439, 5664, 5824, 5832, 8526, 12985, 15625, 15698, 19435, 25284, 25662]
In base 10: [[1, 5, 6, 8], [2, 8, 3, 5], [4, 7, 5, 2], [5, 2, 6, 5], [5, 4, 3, 9], [5, 6, 6, 4], [5, 8, 2, 4], [5, 8, 3, 2], [8, 5, 2, 6], [1, 2, 9, 8, 5], [1, 5, 6, 2, 5], [1, 5, 6, 9, 8], [1, 9, 4, 3, 5], [2, 5, 2, 8, 4], [2, 5, 6, 6, 2]]
 
 
First 15 Rhondas in base 12:
In base 10: [560, 800, 3993, 4425, 4602, 4888, 7315, 8296, 9315, 11849, 12028, 13034, 14828, 15052, 16264]
In base 12: [[3, 10, 8], [5, 6, 8], [2, 3, 8, 9], [2, 6, 8, 9], [2, 7, 11, 6], [2, 9, 11, 4], [4, 2, 9, 7], [4, 9, 7, 4], [5, 4, 8, 3], [6, 10, 3, 5], [6, 11, 6, 4], [7, 6, 6, 2], [8, 6, 11, 8], [8, 8, 6, 4], [9, 4, 11, 4]]
 
 
First 15 Rhondas in base 14:
In base 10: [11475, 18655, 20565, 29631, 31725, 45387, 58404, 58667, 59950, 63945, 67525, 68904, 91245, 99603, 125543]
In base 14: [[4, 2, 7, 9], [6, 11, 2, 7], [7, 6, 12, 13], [10, 11, 2, 7], [11, 7, 12, 1], [1, 2, 7, 7, 13], [1, 7, 3, 13, 10], [1, 7, 5, 4, 7], [1, 7, 11, 12, 2], [1, 9, 4, 3, 7], [1, 10, 8, 7, 3], [1, 11, 1, 7, 10], [2, 5, 3, 7, 7], [2, 8, 4, 2, 7], [3, 3, 10, 7, 5]]
 
 
First 15 Rhondas in base 15:
In base 10: [2392, 2472, 11468, 15873, 17424, 18126, 19152, 20079, 24388, 30758, 31150, 33004, 33550, 37925, 39483]
In base 15: [[10, 9, 7], [10, 14, 12], [3, 5, 14, 8], [4, 10, 8, 3], [5, 2, 6, 9], [5, 5, 8, 6], [5, 10, 1, 12], [5, 14, 3, 9], [7, 3, 5, 13], [9, 1, 10, 8], [9, 3, 6, 10], [9, 11, 10, 4], [9, 14, 1, 10], [11, 3, 8, 5], [11, 10, 7, 3]]
 
 
First 15 Rhondas in base 16:
In base 10: [1000, 1134, 6776, 15912, 19624, 20043, 20355, 23946, 26296, 29070, 31906, 32292, 34236, 34521, 36465]
In base 16: [[3, 14, 8], [4, 6, 14], [1, 10, 7, 8], [3, 14, 2, 8], [4, 12, 10, 8], [4, 14, 4, 11], [4, 15, 8, 3], [5, 13, 8, 10], [6, 6, 11, 8], [7, 1, 8, 14], [7, 12, 10, 2], [7, 14, 2, 4], [8, 5, 11, 12], [8, 6, 13, 9], [8, 14, 7, 1]]
 
 
 
</pre>
 
=={{header|Perl}}==
Line 1,849 ⟶ 2,197:
In base 36: rs 3pc 4di 6bi 8hi 9ks a9g c5i cz9 hrc 13to 14ou 1g9s 1iq9 1lw6
</pre>
=={{header|Python}}==
 
<syntaxhighlight lang="python">
# rhonda.py by Xing216
def prime_factors_sum(n):
i = 2
factors_sum = 0
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors_sum+=i
if n > 1:
factors_sum+=n
return factors_sum
def digits_product(n: int, base: int):
# translated from the nim solution
i = 1
while n != 0:
i *= n % base
n //= base
return i
def is_rhonda_num(n:int, base: int):
product = digits_product(n, base)
return product == base * prime_factors_sum(n)
def convert_base(num,b):
numerals="0123456789abcdefghijklmnopqrstuvwxyz"
return ((num == 0) and numerals[0]) or (convert_base(num // b, b).lstrip(numerals[0]) + numerals[num % b])
def is_prime(n):
if n == 1:
return False
i = 2
while i*i <= n:
if n % i == 0:
return False
i += 1
return True
for base in range(4,37):
rhonda_nums = []
if is_prime(base):
continue
i = 1
while len(rhonda_nums) < 10:
if is_rhonda_num(i,base) :
rhonda_nums.append(i)
i+=1
else:
i+=1
print(f"base {base}: {', '.join([convert_base(n, base) for n in rhonda_nums])}")
</syntaxhighlight>
{{out}}
<pre style="height:40ex;overflow:scroll;">
base 4: 2133132, 2322133, 2331312, 3322133, 22333212, 112333221, 123211332, 123323233, 232222231, 323233221
base 6: 3543, 4433, 25353, 41453, 52332, 53452, 153532, 224332, 431354, 443132
base 8: 3454, 14256, 14736, 24442, 34244, 34623, 42367, 44166, 61466, 62544
base 9: 23276, 31783, 37665, 66758, 67232, 67323, 72326, 76317, 83328, 126376
base 10: 1568, 2835, 4752, 5265, 5439, 5664, 5824, 5832, 8526, 12985
base 12: 3a8, 568, 2389, 2689, 27b6, 29b4, 4297, 4974, 5483, 6a35
base 14: 4279, 6b27, 76cd, ab27, b7c1, 1277d, 173da, 17547, 17bc2, 19437
base 15: a97, aec, 35e8, 4a83, 5269, 5586, 5a1c, 5e39, 735d, 91a8
base 16: 3e8, 46e, 1a78, 3e28, 4ca8, 4e4b, 4f83, 5d8a, 66b8, 718e
base 18: 49c, 94c, 1998, 2g9f, 35fg, 39d4, 3b36, 3e6g, 49f8, 64e9
base 20: 4af, 17ca, 1i4f, 2ci5, 2f85, 3gf2, 465a, 46c5, 55ec, 5a85
base 21: 3ef, c4e, j67, 189e, 1ebc, 2eg6, 33ec, 3e2i, 45e9, 55i7
base 22: 5cb, 8be, g5b, 2fb2, 2lb8, 3ab4, 6gb1, 6lbc, b16g, b1cj
base 24: 3eg, 4gl, 6lg, 9ic, 9jg, c9g, e9g, fg6, hce, 16dk
base 25: ake, fa8, l5a, 1a5m, 3aa7, 3h5f, 45ff, 4aa6, 655e, 8o55
base 26: bde, dke, dme, gd6, 16kd, 1f6d, 1pgd, 2e6d, 2i2d, 2kmd
base 27: 6fi, g6i, gf9, i2o, k9i, o9b, 169k, 19ni, 1aii, 29jf
base 28: 3qe, 7bc, 7c8, 9ee, e6g, hc7, 16lq, 17qq, 18em, 1m67
base 30: 3ao, 3fi, 5kf, 5s6, 6ia, 7fc, 8ia, 8p6, 9ca, afq
base 32: 1so, 3gg, d6g, fg4, gas, geh, oq2, p4o, s8n, 1ebg
base 33: mu, 6fb, 6vb, cmw, mtf, s3m, 1lgb, 1pbu, 1q3m, 3lml
base 34: 4uh, c8h, dhe, e8h, j6h, w4h, 36lh, 3ehi, 3f4h, 3hqo
base 35: 6p7, 7pq, 7u8, f7e, p9e, y7a, 17lu, 17sa, 1bfe, 1fl7
base 36: rs, 3pc, 4di, 6bi, 8hi, 9ks, a9g, c5i, cz9, hrc
</pre>
=={{header|Raku}}==
Find and show the first 15 so as to display the namesake Rhonda number 25662.
Line 2,352 ⟶ 2,776:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Math, Int, Nums
import "./fmt" for Fmt, Conv
 
35

edits