Rhonda numbers: Difference between revisions

Added Python implementation for Rhonda Numbers task
(Add PARI/GP implementation)
(Added Python implementation for Rhonda Numbers task)
 
(One intermediate revision by one other user 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 2,115 ⟶ 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.
35

edits