Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Rhonda-numbers/00-META.yaml
Normal file
2
Task/Rhonda-numbers/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Rhonda_numbers
|
||||
48
Task/Rhonda-numbers/00-TASK.txt
Normal file
48
Task/Rhonda-numbers/00-TASK.txt
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
A positive integer '''''n''''' is said to be a Rhonda number to base '''''b''''' if the product of the base '''''b''''' digits of '''''n''''' is equal to '''''b''''' times the sum of '''''n''''''s prime factors.
|
||||
|
||||
|
||||
''These numbers were named by Kevin Brown after an acquaintance of his whose residence number was 25662, a member of the base 10 numbers with this property.''
|
||||
|
||||
|
||||
'''25662''' is a Rhonda number to base-'''10'''. The prime factorization is '''2 × 3 × 7 × 13 × 47'''; the product of its base-'''10''' digits is equal to the base times the sum of its prime factors:
|
||||
|
||||
<span style=font-size:150%;font-weight:bold;padding-left:3em;>2 × 5 × 6 × 6 × 2 = 720 = 10 × (2 + 3 + 7 + 13 + 47)</span>
|
||||
|
||||
Rhonda numbers only exist in bases that are not a prime.
|
||||
|
||||
''Rhonda numbers to base 10 '''always''' contain at least 1 digit 5 and '''always''' contain at least 1 even digit.''
|
||||
|
||||
|
||||
;Task
|
||||
|
||||
* For the non-prime bases '''''b''''' from '''2''' through '''16''' , find and display here, on this page, at least the first '''10''' '''Rhonda numbers''' to base '''''b'''''. Display the found numbers at least in base '''10'''.
|
||||
|
||||
|
||||
;Stretch
|
||||
|
||||
* Extend out to base '''36'''.
|
||||
|
||||
|
||||
;See also
|
||||
|
||||
;* [https://mathworld.wolfram.com/RhondaNumber.html Wolfram Mathworld - Rhonda numbers]
|
||||
;* [https://www.numbersaplenty.com/set/Rhonda_number Numbers Aplenty - Rhonda numbers]
|
||||
;* [[oeis:A100968|OEIS:A100968 - Integers n that are Rhonda numbers to base 4]]
|
||||
;* [[oeis:A100969|OEIS:A100969 - Integers n that are Rhonda numbers to base 6]]
|
||||
;* [[oeis:A100970|OEIS:A100970 - Integers n that are Rhonda numbers to base 8]]
|
||||
;* [[oeis:A100973|OEIS:A100973 - Integers n that are Rhonda numbers to base 9]]
|
||||
;* [[oeis:A099542|OEIS:A099542 - Rhonda numbers to base 10]]
|
||||
;* [[oeis:A100971|OEIS:A100971 - Integers n that are Rhonda numbers to base 12]]
|
||||
;* [[oeis:A100972|OEIS:A100972 - Integers n that are Rhonda numbers to base 14]]
|
||||
;* [[oeis:A100974|OEIS:A100974 - Integers n that are Rhonda numbers to base 15]]
|
||||
;* [[oeis:A100975|OEIS:A100975 - Integers n that are Rhonda numbers to base 16]]
|
||||
|
||||
;* [[oeis:A255735|OEIS:A255735 - Integers n that are Rhonda numbers to base 18]]
|
||||
;* [[oeis:A255732|OEIS:A255732 - Rhonda numbers in vigesimal number system]] (base 20)
|
||||
;* [[oeis:A255736|OEIS:A255736 - Integers that are Rhonda numbers to base 30]]
|
||||
|
||||
;* [[Smith numbers|Related Task: Smith numbers]]
|
||||
<br>
|
||||
|
||||
|
||||
|
||||
97
Task/Rhonda-numbers/ALGOL-68/rhonda-numbers.alg
Normal file
97
Task/Rhonda-numbers/ALGOL-68/rhonda-numbers.alg
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
BEGIN # find some Rhonda numbers: numbers n in base b such that the product #
|
||||
# of the digits of n is b * the sum of the prime factors of n #
|
||||
|
||||
# returns the sum of the prime factors of n #
|
||||
PROC factor sum = ( INT n )INT:
|
||||
BEGIN
|
||||
INT result := 0;
|
||||
INT v := ABS n;
|
||||
WHILE v > 1 AND v MOD 2 = 0 DO
|
||||
result +:= 2;
|
||||
v OVERAB 2
|
||||
OD;
|
||||
FOR f FROM 3 BY 2 WHILE v > 1 DO
|
||||
WHILE v > 1 AND v MOD f = 0 DO
|
||||
result +:= f;
|
||||
v OVERAB f
|
||||
OD
|
||||
OD;
|
||||
result
|
||||
END # factor sum # ;
|
||||
# returns the digit product of n in the specified base #
|
||||
PROC digit product = ( INT n, base )INT:
|
||||
IF n = 0 THEN 0
|
||||
ELSE
|
||||
INT result := 1;
|
||||
INT v := ABS n;
|
||||
WHILE v > 0 DO
|
||||
result *:= v MOD base;
|
||||
v OVERAB base
|
||||
OD;
|
||||
result
|
||||
FI # digit product # ;
|
||||
# returns TRUE if n is a Rhonda number in the specified base, #
|
||||
# FALSE otherwise #
|
||||
PROC is rhonda = ( INT n, base )BOOL: base * factor sum( n ) = digit product( n, base );
|
||||
|
||||
# returns TRUE if n is prime, FALSE otherwise #
|
||||
PROC is prime = ( INT n )BOOL:
|
||||
IF n < 3 THEN n = 2
|
||||
ELIF n MOD 3 = 0 THEN n = 3
|
||||
ELIF NOT ODD n THEN FALSE
|
||||
ELSE
|
||||
INT f := 5;
|
||||
INT f2 := 25;
|
||||
INT to next := 24;
|
||||
BOOL is a prime := TRUE;
|
||||
WHILE f2 <= n AND is a prime DO
|
||||
is a prime := n MOD f /= 0;
|
||||
f +:= 2;
|
||||
f2 +:= to next;
|
||||
to next +:= 8
|
||||
OD;
|
||||
is a prime
|
||||
FI # is prime # ;
|
||||
# returns a string representation of n in the specified base #
|
||||
PROC to base string = ( INT n, base )STRING:
|
||||
IF n = 0 THEN "0"
|
||||
ELSE
|
||||
INT under 10 = ABS "0";
|
||||
INT over 9 = ABS "a" - 10;
|
||||
STRING result := "";
|
||||
INT v := ABS n;
|
||||
WHILE v > 0 DO
|
||||
INT d = v MOD base;
|
||||
REPR ( d + IF d < 10 THEN under 10 ELSE over 9 FI ) +=: result;
|
||||
v OVERAB base
|
||||
OD;
|
||||
result
|
||||
FI # to base string # ;
|
||||
# find the first few Rhonda numbers in non-prime bases 2 .. max base #
|
||||
INT max rhonda = 10;
|
||||
INT max base = 16;
|
||||
FOR base FROM 2 TO max base DO
|
||||
IF NOT is prime( base ) THEN
|
||||
print( ( "The first ", whole( max rhonda, 0 )
|
||||
, " Rhonda numbers in base ", whole( base, 0 )
|
||||
, ":", newline
|
||||
)
|
||||
);
|
||||
INT r count := 0;
|
||||
[ 1 : max rhonda ]INT rhonda;
|
||||
FOR n WHILE r count < max rhonda DO
|
||||
IF is rhonda( n, base ) THEN
|
||||
rhonda[ r count +:= 1 ] := n
|
||||
FI
|
||||
OD;
|
||||
print( ( " in base 10:" ) );
|
||||
FOR i TO max rhonda DO print( ( " ", whole( rhonda[ i ], 0 ) ) ) OD;
|
||||
print( ( newline ) );
|
||||
IF base /= 10 THEN
|
||||
print( ( " in base ", whole( base, -2 ), ":" ) );
|
||||
FOR i TO max rhonda DO print( ( " ", to base string( rhonda[ i ], base ) ) ) OD;
|
||||
print( ( newline ) )
|
||||
FI
|
||||
FI
|
||||
OD
|
||||
END
|
||||
18
Task/Rhonda-numbers/Arturo/rhonda-numbers.arturo
Normal file
18
Task/Rhonda-numbers/Arturo/rhonda-numbers.arturo
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
digs: (@`0`..`9`) ++ @`A`..`Z`
|
||||
toBase: function [n,base][
|
||||
join map digits.base:base n 'x -> digs\[x]
|
||||
]
|
||||
|
||||
rhonda?: function [n,base][
|
||||
(base * sum factors.prime n) = product digits.base:base n
|
||||
]
|
||||
|
||||
nonPrime: select 2..16 'x -> not? prime? x
|
||||
|
||||
loop nonPrime 'npbase [
|
||||
print "The first 10 Rhonda numbers, base-" ++ (to :string npbase) ++ ":"
|
||||
rhondas: select.first:10 1..∞ 'z -> rhonda? z npbase
|
||||
print ["In base 10 ->" join.with:", " to [:string] rhondas]
|
||||
print ["In base" npbase "->" join.with:", " to [:string] map rhondas 'w -> toBase w npbase]
|
||||
print ""
|
||||
]
|
||||
76
Task/Rhonda-numbers/C++/rhonda-numbers.cpp
Normal file
76
Task/Rhonda-numbers/C++/rhonda-numbers.cpp
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
int digit_product(int base, int n) {
|
||||
int product = 1;
|
||||
for (; n != 0; n /= base)
|
||||
product *= n % base;
|
||||
return product;
|
||||
}
|
||||
|
||||
int prime_factor_sum(int n) {
|
||||
int sum = 0;
|
||||
for (; (n & 1) == 0; n >>= 1)
|
||||
sum += 2;
|
||||
for (int p = 3; p * p <= n; p += 2)
|
||||
for (; n % p == 0; n /= p)
|
||||
sum += p;
|
||||
if (n > 1)
|
||||
sum += n;
|
||||
return sum;
|
||||
}
|
||||
|
||||
bool is_prime(int n) {
|
||||
if (n < 2)
|
||||
return false;
|
||||
if (n % 2 == 0)
|
||||
return n == 2;
|
||||
if (n % 3 == 0)
|
||||
return n == 3;
|
||||
for (int p = 5; p * p <= n; p += 4) {
|
||||
if (n % p == 0)
|
||||
return false;
|
||||
p += 2;
|
||||
if (n % p == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_rhonda(int base, int n) {
|
||||
return digit_product(base, n) == base * prime_factor_sum(n);
|
||||
}
|
||||
|
||||
std::string to_string(int base, int n) {
|
||||
assert(base <= 36);
|
||||
static constexpr char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
std::string str;
|
||||
for (; n != 0; n /= base)
|
||||
str += digits[n % base];
|
||||
std::reverse(str.begin(), str.end());
|
||||
return str;
|
||||
}
|
||||
|
||||
int main() {
|
||||
const int limit = 15;
|
||||
for (int base = 2; base <= 36; ++base) {
|
||||
if (is_prime(base))
|
||||
continue;
|
||||
std::cout << "First " << limit << " Rhonda numbers to base " << base
|
||||
<< ":\n";
|
||||
int numbers[limit];
|
||||
for (int n = 1, count = 0; count < limit; ++n) {
|
||||
if (is_rhonda(base, n))
|
||||
numbers[count++] = n;
|
||||
}
|
||||
std::cout << "In base 10:";
|
||||
for (int i = 0; i < limit; ++i)
|
||||
std::cout << ' ' << numbers[i];
|
||||
std::cout << "\nIn base " << base << ':';
|
||||
for (int i = 0; i < limit; ++i)
|
||||
std::cout << ' ' << to_string(base, numbers[i]);
|
||||
std::cout << "\n\n";
|
||||
}
|
||||
}
|
||||
19
Task/Rhonda-numbers/Factor/rhonda-numbers.factor
Normal file
19
Task/Rhonda-numbers/Factor/rhonda-numbers.factor
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
USING: formatting grouping io kernel lists lists.lazy math
|
||||
math.parser math.primes math.primes.factors prettyprint ranges
|
||||
sequences sequences.extras ;
|
||||
|
||||
: rhonda? ( n base -- ? )
|
||||
[ [ >base 1 group ] keep '[ _ base> ] map-product ]
|
||||
[ swap factors sum * ] 2bi = ;
|
||||
|
||||
: rhonda ( base -- list ) 1 lfrom swap '[ _ rhonda? ] lfilter ;
|
||||
|
||||
: list. ( list base -- ) '[ _ >base write bl ] leach nl ;
|
||||
|
||||
:: rhonda. ( base -- )
|
||||
15 base rhonda ltake :> r
|
||||
base "First 15 Rhonda numbers to base %d:\n" printf
|
||||
"In base 10: " write r 10 list.
|
||||
base "In base %d: " printf r base list. ;
|
||||
|
||||
2 36 [a..b] [ prime? not ] filter [ rhonda. nl ] each
|
||||
73
Task/Rhonda-numbers/Go/rhonda-numbers.go
Normal file
73
Task/Rhonda-numbers/Go/rhonda-numbers.go
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"rcu"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func contains(a []int, n int) bool {
|
||||
for _, e := range a {
|
||||
if e == n {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func main() {
|
||||
for b := 2; b <= 36; b++ {
|
||||
if rcu.IsPrime(b) {
|
||||
continue
|
||||
}
|
||||
count := 0
|
||||
var rhonda []int
|
||||
for n := 1; count < 15; n++ {
|
||||
digits := rcu.Digits(n, b)
|
||||
if !contains(digits, 0) {
|
||||
var anyEven = false
|
||||
for _, d := range digits {
|
||||
if d%2 == 0 {
|
||||
anyEven = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if b != 10 || (contains(digits, 5) && anyEven) {
|
||||
calc1 := 1
|
||||
for _, d := range digits {
|
||||
calc1 *= d
|
||||
}
|
||||
calc2 := b * rcu.SumInts(rcu.PrimeFactors(n))
|
||||
if calc1 == calc2 {
|
||||
rhonda = append(rhonda, n)
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(rhonda) > 0 {
|
||||
fmt.Printf("\nFirst 15 Rhonda numbers in base %d:\n", b)
|
||||
rhonda2 := make([]string, len(rhonda))
|
||||
counts2 := make([]int, len(rhonda))
|
||||
for i, r := range rhonda {
|
||||
rhonda2[i] = fmt.Sprintf("%d", r)
|
||||
counts2[i] = len(rhonda2[i])
|
||||
}
|
||||
rhonda3 := make([]string, len(rhonda))
|
||||
counts3 := make([]int, len(rhonda))
|
||||
for i, r := range rhonda {
|
||||
rhonda3[i] = strconv.FormatInt(int64(r), b)
|
||||
counts3[i] = len(rhonda3[i])
|
||||
}
|
||||
maxLen2 := rcu.MaxInts(counts2)
|
||||
maxLen3 := rcu.MaxInts(counts3)
|
||||
maxLen := maxLen2
|
||||
if maxLen3 > maxLen {
|
||||
maxLen = maxLen3
|
||||
}
|
||||
maxLen++
|
||||
fmt.Printf("In base 10: %*s\n", maxLen, rhonda2)
|
||||
fmt.Printf("In base %-2d: %*s\n", b, maxLen, rhonda3)
|
||||
}
|
||||
}
|
||||
}
|
||||
129
Task/Rhonda-numbers/Hoon/rhonda-numbers-1.hoon
Normal file
129
Task/Rhonda-numbers/Hoon/rhonda-numbers-1.hoon
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
::
|
||||
:: A library for producing Rhonda numbers and testing if numbers are Rhonda.
|
||||
::
|
||||
:: A number is Rhonda if the product of its digits of in base b equals
|
||||
:: the product of the base b and the sum of its prime factors.
|
||||
:: see also: https://mathworld.wolfram.com/RhondaNumber.html
|
||||
::
|
||||
=<
|
||||
::
|
||||
|%
|
||||
:: +check: test whether the number n is Rhonda to base b
|
||||
::
|
||||
++ check
|
||||
|= [b=@ud n=@ud]
|
||||
^- ?
|
||||
~_ leaf+"base b must be >= 2"
|
||||
?> (gte b 2)
|
||||
~_ leaf+"candidate number n must be >= 2"
|
||||
?> (gte n 2)
|
||||
::
|
||||
.= (roll (base-digits b n) mul)
|
||||
%+ mul
|
||||
b
|
||||
(roll (prime-factors n) add)
|
||||
:: +series: produce the first n numbers which are Rhonda in base b
|
||||
::
|
||||
:: produce ~ if base b has no Rhonda numbers
|
||||
::
|
||||
++ series
|
||||
|= [b=@ud n=@ud]
|
||||
^- (list @ud)
|
||||
~_ leaf+"base b must be >= 2"
|
||||
?> (gte b 2)
|
||||
::
|
||||
?: =((prime-factors b) ~[b])
|
||||
~
|
||||
=/ candidate=@ud 2
|
||||
=+ rhondas=*(list @ud)
|
||||
|-
|
||||
?: =(n 0)
|
||||
(flop rhondas)
|
||||
=/ is-rhonda=? (check b candidate)
|
||||
%= $
|
||||
rhondas ?:(is-rhonda [candidate rhondas] rhondas)
|
||||
n ?:(is-rhonda (dec n) n)
|
||||
candidate +(candidate)
|
||||
==
|
||||
--
|
||||
::
|
||||
|%
|
||||
:: +base-digits: produce a list of the digits of n represented in base b
|
||||
::
|
||||
:: This arm has two behaviors which may be at first surprising, but do not
|
||||
:: matter for the purposes of the ++check and ++series arms, and allow for
|
||||
:: some simplifications to its implementation.
|
||||
:: - crashes on n=0
|
||||
:: - orders the list of digits with least significant digits first
|
||||
::
|
||||
:: ex: (base-digits 4 10.206) produces ~[2 3 1 3 3 1 2]
|
||||
::
|
||||
++ base-digits
|
||||
|= [b=@ud n=@ud]
|
||||
^- (list @ud)
|
||||
?> (gte b 2)
|
||||
?< =(n 0)
|
||||
::
|
||||
|-
|
||||
?: =(n 0)
|
||||
~
|
||||
:- (mod n b)
|
||||
$(n (div n b))
|
||||
:: +prime-factors: produce a list of the prime factors of n
|
||||
::
|
||||
:: by trial division
|
||||
:: n must be >= 2
|
||||
:: if n is prime, produce ~[n]
|
||||
:: ex: (prime-factors 10.206) produces ~[7 3 3 3 3 3 3 2]
|
||||
::
|
||||
++ prime-factors
|
||||
|= [n=@ud]
|
||||
^- (list @ud)
|
||||
?> (gte n 2)
|
||||
::
|
||||
=+ factors=*(list @ud)
|
||||
=/ wheel new-wheel
|
||||
:: test candidates as produced by the wheel, not exceeding sqrt(n)
|
||||
::
|
||||
|-
|
||||
=^ candidate wheel (next:wheel)
|
||||
?. (lte (mul candidate candidate) n)
|
||||
?:((gth n 1) [n factors] factors)
|
||||
|-
|
||||
?: =((mod n candidate) 0)
|
||||
:: repeat the prime factor as many times as possible
|
||||
::
|
||||
$(factors [candidate factors], n (div n candidate))
|
||||
^$
|
||||
:: +new-wheel: a door for generating numbers that may be prime
|
||||
::
|
||||
:: This uses wheel factorization with a basis of {2, 3, 5} to limit the
|
||||
:: number of composites produced. It produces numbers in increasing order
|
||||
:: starting from 2.
|
||||
::
|
||||
++ new-wheel
|
||||
=/ fixed=(list @ud) ~[2 3 5 7]
|
||||
=/ skips=(list @ud) ~[4 2 4 2 4 6 2 6]
|
||||
=/ lent-fixed=@ud (lent fixed)
|
||||
=/ lent-skips=@ud (lent skips)
|
||||
::
|
||||
|_ [current=@ud fixed-i=@ud skips-i=@ud]
|
||||
:: +next: produce the next number and the new wheel state
|
||||
::
|
||||
++ next
|
||||
|.
|
||||
:: Exhaust the numbers in fixed. Then calculate successive values by
|
||||
:: cycling through skips and increasing from the previous number by
|
||||
:: the current skip-value.
|
||||
::
|
||||
=/ fixed-done=? =(fixed-i lent-fixed)
|
||||
=/ next-fixed-i ?:(fixed-done fixed-i +(fixed-i))
|
||||
=/ next-skips-i ?:(fixed-done (mod +(skips-i) lent-skips) skips-i)
|
||||
=/ next
|
||||
?. fixed-done
|
||||
(snag fixed-i fixed)
|
||||
(add current (snag skips-i skips))
|
||||
:- next
|
||||
+.$(current next, fixed-i next-fixed-i, skips-i next-skips-i)
|
||||
--
|
||||
--
|
||||
5
Task/Rhonda-numbers/Hoon/rhonda-numbers-2.hoon
Normal file
5
Task/Rhonda-numbers/Hoon/rhonda-numbers-2.hoon
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
/+ *rhonda
|
||||
:- %say
|
||||
|= [* [base=@ud many=@ud ~] ~]
|
||||
:- %noun
|
||||
(series base many)
|
||||
164
Task/Rhonda-numbers/Hoon/rhonda-numbers-3.hoon
Normal file
164
Task/Rhonda-numbers/Hoon/rhonda-numbers-3.hoon
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
|%
|
||||
++ check
|
||||
|= [n=@ud base=@ud]
|
||||
:: if base is prime, automatic no
|
||||
::
|
||||
?: =((~(gut by (prime-map +(base))) base 0) 0)
|
||||
%.n
|
||||
:: if not multiply the digits and compare to base x sum of factors
|
||||
::
|
||||
?: =((roll (digits [base n]) mul) (mul base (roll (factor n) add)))
|
||||
%.y
|
||||
%.n
|
||||
++ series
|
||||
|= [base=@ud many=@ud]
|
||||
=/ rhondas *(list @ud)
|
||||
?: =((~(gut by (prime-map +(base))) base 0) 0)
|
||||
rhondas
|
||||
=/ itr 1
|
||||
|-
|
||||
?: =((lent rhondas) many)
|
||||
(flop rhondas)
|
||||
?: =((check itr base) %.n)
|
||||
$(itr +(itr))
|
||||
$(rhondas [itr rhondas], itr +(itr))
|
||||
:: digits: gives the list of digits of a number in a base
|
||||
::
|
||||
:: We strip digits least to most significant.
|
||||
:: The least significant digit (lsd) of n in base b is just n mod b.
|
||||
:: Subtract the lsd, divide by b, and repeat.
|
||||
:: To know when to stop, we need to know how many digits there are.
|
||||
++ digits
|
||||
|= [base=@ud num=@ud]
|
||||
^- (list @ud)
|
||||
|-
|
||||
=/ modulus=@ud (mod num base)
|
||||
?: =((num-digits base num) 1)
|
||||
~[modulus]
|
||||
[modulus $(num (div (sub num modulus) base))]
|
||||
:: num-digits: gives the number of digits of a number in a base
|
||||
::
|
||||
:: Simple idea: k is the number of digits of n in base b if and
|
||||
:: only if k is the smallest number such that b^k > n.
|
||||
++ num-digits
|
||||
|= [base=@ud num=@ud]
|
||||
^- @ud
|
||||
=/ digits=@ud 1
|
||||
|-
|
||||
?: (gth (pow base digits) num)
|
||||
digits
|
||||
$(digits +(digits))
|
||||
:: factor: produce a list of prime factors
|
||||
::
|
||||
:: The idea is to identify "small factors" of n, i.e. prime factors less than
|
||||
:: the square root. We then divide n by these factors to reduce the
|
||||
:: magnitude of n. It's easy to argue that after this is done, we obtain 1
|
||||
:: or the largest prime factor.
|
||||
::
|
||||
++ factor
|
||||
|= n=@ud
|
||||
^- (list @ud)
|
||||
?: ?|(=(n 0) =(n 1))
|
||||
~[n]
|
||||
=/ factorization *(list @ud)
|
||||
:: produce primes less than or equal to root n
|
||||
::
|
||||
=/ root (sqrt n)
|
||||
=/ primes (prime-map +(root))
|
||||
:: itr = iterate; we want to iterate through the primes less than root n
|
||||
::
|
||||
=/ itr 2
|
||||
|-
|
||||
?: =(itr +(root))
|
||||
:: if n is now 1 we're done
|
||||
::
|
||||
?: =(n 1)
|
||||
factorization
|
||||
:: otherwise it's now the original n's largest primes factor
|
||||
::
|
||||
[n factorization]
|
||||
:: if itr not prime move on
|
||||
::
|
||||
?: =((~(gut by primes) itr 0) 1)
|
||||
$(itr +(itr))
|
||||
:: if it is prime, divide out by the highest power that divides num
|
||||
::
|
||||
?: =((mod n itr) 0)
|
||||
$(n (div n itr), factorization [itr factorization])
|
||||
:: once done, move to next prime
|
||||
::
|
||||
$(itr +(itr))
|
||||
:: sqrt: gives the integer square root of a number
|
||||
::
|
||||
:: It's based on an algorithm that predates the Greeks:
|
||||
:: To find the square root of A, think of A as an area.
|
||||
:: Guess the side of the square x. Compute the other side y = A/x.
|
||||
:: If x is an over/underestimate then y is an under/overestimate.
|
||||
:: So (x+y)/2 is the average of an over and underestimate, thus better than x.
|
||||
:: Repeatedly doing x --> (x + A/x)/2 converges to sqrt(A).
|
||||
::
|
||||
:: This algorithm is the same but with integer valued operations.
|
||||
:: The algorithm either converges to the integer square root and repeats,
|
||||
:: or gets trapped in a two-cycle of adjacent integers.
|
||||
:: In the latter case, the smaller number is the answer.
|
||||
::
|
||||
++ sqrt
|
||||
|= n=@ud
|
||||
=/ guess=@ud 1
|
||||
|-
|
||||
=/ new-guess (div (add guess (div n guess)) 2)
|
||||
:: sequence stabilizes
|
||||
::
|
||||
?: =(guess new-guess)
|
||||
guess
|
||||
:: sequence is trapped in 2-cycle
|
||||
::
|
||||
?: =(guess +(new-guess))
|
||||
new-guess
|
||||
?: =(new-guess +(guess))
|
||||
guess
|
||||
$(guess new-guess)
|
||||
:: prime-map: (effectively) produces primes less than a given input
|
||||
::
|
||||
:: This is the sieve of Eratosthenes to produce primes less than n.
|
||||
:: I used a map because it had much faster performance than a list.
|
||||
:: Any key in the map is a non-prime. The value 1 indicates "false."
|
||||
:: I.e. it's not a prime.
|
||||
++ prime-map
|
||||
|= n=@ud
|
||||
^- (map @ud @ud)
|
||||
=/ prime-map `(map @ud @ud)`(my ~[[0 1] [1 1]])
|
||||
:: start sieving with 2
|
||||
::
|
||||
=/ sieve 2
|
||||
|-
|
||||
:: if sieve is too large to be a factor we're done
|
||||
::
|
||||
?: (gte (mul sieve sieve) n)
|
||||
prime-map
|
||||
:: if not too large but not prime, move on
|
||||
::
|
||||
?: =((~(gut by prime-map) sieve 0) 1)
|
||||
$(sieve +(sieve))
|
||||
:: sequence: explanation
|
||||
::
|
||||
:: If s is the sieve number, we start sieving multiples
|
||||
:: of s at s^2 in sequence: s^2, s^2 + s, s^2 + 2s, ...
|
||||
:: We start at s^2 because any number smaller than s^2
|
||||
:: has prime factors less than s and would have been
|
||||
:: eliminated earlier in the sieving process.
|
||||
::
|
||||
=/ sequence (mul sieve sieve)
|
||||
|-
|
||||
:: done sieving with s once sequence is past n
|
||||
::
|
||||
?: (gte sequence n)
|
||||
^$(sieve +(sieve))
|
||||
:: if sequence position is known not prime we move on
|
||||
::
|
||||
?: =((~(gut by prime-map) sequence 0) 1)
|
||||
$(sequence (add sequence sieve))
|
||||
:: otherwise we mark position of sequence as not prime and move on
|
||||
::
|
||||
$(prime-map (~(put by prime-map) sequence 1), sequence (add sequence sieve))
|
||||
--
|
||||
19
Task/Rhonda-numbers/J/rhonda-numbers.j
Normal file
19
Task/Rhonda-numbers/J/rhonda-numbers.j
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
tobase=: (a.{~;48 97(+ i.)each 10 26) {~ #.inv
|
||||
isrhonda=: (*/@:(#.inv) = (* +/@q:))"0
|
||||
|
||||
task=: {{
|
||||
for_base.(#~ 0=1&p:) }.1+i.36 do.
|
||||
k=.i.0
|
||||
block=. 1+i.1e4
|
||||
while. 15>#k do.
|
||||
k=. k, block#~ base isrhonda block
|
||||
block=. block+1e4
|
||||
end.
|
||||
echo ''
|
||||
echo 'First 15 Rhondas in',b=.' base ',':',~":base
|
||||
echo 'In base 10: ',":15{.k
|
||||
echo 'In',;:inv b;base tobase each 15{.k
|
||||
end.
|
||||
}}
|
||||
|
||||
task''
|
||||
62
Task/Rhonda-numbers/Java/rhonda-numbers.java
Normal file
62
Task/Rhonda-numbers/Java/rhonda-numbers.java
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
public class RhondaNumbers {
|
||||
public static void main(String[] args) {
|
||||
final int limit = 15;
|
||||
for (int base = 2; base <= 36; ++base) {
|
||||
if (isPrime(base))
|
||||
continue;
|
||||
System.out.printf("First %d Rhonda numbers to base %d:\n", limit, base);
|
||||
int numbers[] = new int[limit];
|
||||
for (int n = 1, count = 0; count < limit; ++n) {
|
||||
if (isRhonda(base, n))
|
||||
numbers[count++] = n;
|
||||
}
|
||||
System.out.printf("In base 10:");
|
||||
for (int i = 0; i < limit; ++i)
|
||||
System.out.printf(" %d", numbers[i]);
|
||||
System.out.printf("\nIn base %d:", base);
|
||||
for (int i = 0; i < limit; ++i)
|
||||
System.out.printf(" %s", Integer.toString(numbers[i], base));
|
||||
System.out.printf("\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
private static int digitProduct(int base, int n) {
|
||||
int product = 1;
|
||||
for (; n != 0; n /= base)
|
||||
product *= n % base;
|
||||
return product;
|
||||
}
|
||||
|
||||
private static int primeFactorSum(int n) {
|
||||
int sum = 0;
|
||||
for (; (n & 1) == 0; n >>= 1)
|
||||
sum += 2;
|
||||
for (int p = 3; p * p <= n; p += 2)
|
||||
for (; n % p == 0; n /= p)
|
||||
sum += p;
|
||||
if (n > 1)
|
||||
sum += n;
|
||||
return sum;
|
||||
}
|
||||
|
||||
private static boolean isPrime(int n) {
|
||||
if (n < 2)
|
||||
return false;
|
||||
if (n % 2 == 0)
|
||||
return n == 2;
|
||||
if (n % 3 == 0)
|
||||
return n == 3;
|
||||
for (int p = 5; p * p <= n; p += 4) {
|
||||
if (n % p == 0)
|
||||
return false;
|
||||
p += 2;
|
||||
if (n % p == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isRhonda(int base, int n) {
|
||||
return digitProduct(base, n) == base * primeFactorSum(n);
|
||||
}
|
||||
}
|
||||
32
Task/Rhonda-numbers/Jq/rhonda-numbers-1.jq
Normal file
32
Task/Rhonda-numbers/Jq/rhonda-numbers-1.jq
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
def prod(s): reduce s as $_ (1; . * $_);
|
||||
|
||||
def sigma(s): reduce s as $_ (0; . + $_);
|
||||
|
||||
# If s is a stream of JSON entities that does not include null, butlast(s) emits all but the last.
|
||||
def butlast(s):
|
||||
label $out
|
||||
| foreach (s,null) as $x ({};
|
||||
if $x == null then break $out else .emit = .prev | .prev = $x end)
|
||||
| select(.emit).emit;
|
||||
|
||||
def multiple(s):
|
||||
first(foreach s as $x (0; .+1; select(. > 1))) // false;
|
||||
|
||||
# Output: a stream of the prime factors of the input
|
||||
# e.g.
|
||||
# 2 | factors #=> 2
|
||||
# 24 | factors #=> 2 2 2 3
|
||||
def factors:
|
||||
. as $in
|
||||
| [2, $in, false]
|
||||
| recurse(
|
||||
. as [$p, $q, $valid, $s]
|
||||
| if $q == 1 then empty
|
||||
elif $q % $p == 0 then [$p, $q/$p, true]
|
||||
elif $p == 2 then [3, $q, false, $s]
|
||||
else ($s // ($q | sqrt)) as $s
|
||||
| if $p + 2 <= $s then [$p + 2, $q, false, $s]
|
||||
else [$q, 1, true]
|
||||
end
|
||||
end )
|
||||
| if .[2] then .[0] else empty end ;
|
||||
21
Task/Rhonda-numbers/Jq/rhonda-numbers-2.jq
Normal file
21
Task/Rhonda-numbers/Jq/rhonda-numbers-2.jq
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
|
||||
|
||||
def is_prime:
|
||||
multiple(factors) | not;
|
||||
|
||||
def tobase($b):
|
||||
def digit: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[.:.+1];
|
||||
def mod: . % $b;
|
||||
def div: ((. - mod) / $b);
|
||||
def digits: recurse( select(. > 0) | div) | mod ;
|
||||
# For jq it would be wise to protect against `infinite` as input, but using `isinfinite` confuses gojq
|
||||
select( (tostring|test("^[0-9]+$")) and 2 <= $b and $b <= 36)
|
||||
| if . == 0 then "0"
|
||||
else [digits | digit] | reverse[1:] | add
|
||||
end;
|
||||
|
||||
# emit the decimal values of the "digits"
|
||||
def digits($b):
|
||||
def mod: . % $b;
|
||||
def div: ((. - mod) / $b);
|
||||
butlast(recurse( select(. > 0) | div) | mod) ;
|
||||
8
Task/Rhonda-numbers/Jq/rhonda-numbers-3.jq
Normal file
8
Task/Rhonda-numbers/Jq/rhonda-numbers-3.jq
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Emit a stream of Rhonda numbers in the given base
|
||||
def rhondas($b):
|
||||
range(1; infinite) as $n
|
||||
| ($n | [digits($b)]) as $digits
|
||||
| select($digits|index(0)|not)
|
||||
| select(($b != 10) or (($digits|index(5)) and ($digits | any(. % 2 == 0))))
|
||||
| select(prod($digits[]) == ($b * sigma($n | factors)))
|
||||
| $n ;
|
||||
16
Task/Rhonda-numbers/Jq/rhonda-numbers-4.jq
Normal file
16
Task/Rhonda-numbers/Jq/rhonda-numbers-4.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
def task($count):
|
||||
range (2; 37) as $b
|
||||
| select( $b | is_prime | not)
|
||||
| [ limit($count; rhondas($b)) ]
|
||||
| select(length > 0)
|
||||
|"First \($count) Rhonda numbers in base \($b):",
|
||||
( (map(tostring)) as $rhonda2
|
||||
| (map(tobase($b))) as $rhonda3
|
||||
| (($rhonda2|map(length)) | max) as $maxLen2
|
||||
| (($rhonda3|map(length)) | max) as $maxLen3
|
||||
| ( ([$maxLen2, $maxLen3]|max) + 1) as $maxLen
|
||||
| "In base 10: \($rhonda2 | map(lpad($maxLen)) | join(" ") )",
|
||||
"In base \($b|lpad(2)): \($rhonda3 | map(lpad($maxLen)) | join(" ") )",
|
||||
"") ;
|
||||
|
||||
task(10)
|
||||
18
Task/Rhonda-numbers/Julia/rhonda-numbers.julia
Normal file
18
Task/Rhonda-numbers/Julia/rhonda-numbers.julia
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using Primes
|
||||
|
||||
isRhonda(n, b) = prod(digits(n, base=b)) == b * sum([prod(pair) for pair in factor(n).pe])
|
||||
|
||||
function displayrhondas(low, high, nshow)
|
||||
for b in filter(!isprime, low:high)
|
||||
n, rhondas = 1, Int[]
|
||||
while length(rhondas) < nshow
|
||||
isRhonda(n, b) && push!(rhondas, n)
|
||||
n += 1
|
||||
end
|
||||
println("First $nshow Rhondas in base $b:")
|
||||
println("In base 10: ", rhondas)
|
||||
println("In base $b: ", replace(string([string(i, base=b) for i in rhondas]), "\"" => ""), "\n")
|
||||
end
|
||||
end
|
||||
|
||||
displayrhondas(2, 16, 15)
|
||||
12
Task/Rhonda-numbers/Mathematica/rhonda-numbers.math
Normal file
12
Task/Rhonda-numbers/Mathematica/rhonda-numbers.math
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
ClearAll[RhondaNumberQ]
|
||||
RhondaNumberQ[b_Integer][n_Integer] := Module[{l, r},
|
||||
l = Times @@ IntegerDigits[n, b];
|
||||
r = Total[Catenate[ConstantArray @@@ FactorInteger[n]]];
|
||||
l == b r
|
||||
]
|
||||
bases = Select[Range[2, 36], PrimeQ/*Not];
|
||||
Do[
|
||||
Print["base ", b, ":", Take[Select[Range[700000], RhondaNumberQ[b]], UpTo[15]]];
|
||||
,
|
||||
{b, bases}
|
||||
]
|
||||
73
Task/Rhonda-numbers/Nim/rhonda-numbers.nim
Normal file
73
Task/Rhonda-numbers/Nim/rhonda-numbers.nim
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
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()
|
||||
22
Task/Rhonda-numbers/Perl/rhonda-numbers.pl
Normal file
22
Task/Rhonda-numbers/Perl/rhonda-numbers.pl
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use feature 'say';
|
||||
use ntheory qw<is_prime factor vecsum vecprod todigitstring todigits>;
|
||||
|
||||
sub rhonda {
|
||||
my($b, $cnt) = @_;
|
||||
my(@r,$n);
|
||||
while (++$n) {
|
||||
push @r, $n if ($b * vecsum factor($n)) == vecprod todigits($n,$b);
|
||||
return @r if $cnt == @r;
|
||||
}
|
||||
}
|
||||
|
||||
for my $b (grep { ! is_prime $_ } 2..36) {
|
||||
my @Rb = map { todigitstring($_,$b) } my @R = rhonda($b, 15);
|
||||
say <<~EOT;
|
||||
First 15 Rhonda numbers to base $b:
|
||||
In base $b: @Rb
|
||||
In base 10: @R
|
||||
EOT
|
||||
}
|
||||
34
Task/Rhonda-numbers/Phix/rhonda-numbers.phix
Normal file
34
Task/Rhonda-numbers/Phix/rhonda-numbers.phix
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
|
||||
First 15 Rhonda numbers in base %d:
|
||||
In base 10: %s
|
||||
In base %-2d: %s
|
||||
|
||||
"""</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">-</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;"><=</span><span style="color: #008000;">'9'</span><span style="color: #0000FF;">?</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">:</span><span style="color: #008000;">'a'</span><span style="color: #0000FF;">-</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">36</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rhondab</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000080;font-style:italic;">-- (base)</span>
|
||||
<span style="color: #000000;">rhondad</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> <span style="color: #000080;font-style:italic;">-- (decimal)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rhondab</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">15</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%a"</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">base</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">}})</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">base</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">10</span> <span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'5'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">even</span><span style="color: #0000FF;">))!=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">pd</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">product</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">)),</span>
|
||||
<span style="color: #000000;">bs</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">pd</span><span style="color: #0000FF;">==</span><span style="color: #000000;">bs</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">decdig</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">decdig</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #000000;">rhondab</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rhondab</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">pad_head</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #000000;">rhondad</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rhondad</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">pad_head</span><span style="color: #0000FF;">(</span><span style="color: #000000;">decdig</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">base</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rhondad</span><span style="color: #0000FF;">),</span><span style="color: #000000;">base</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rhondab</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
17
Task/Rhonda-numbers/Raku/rhonda-numbers.raku
Normal file
17
Task/Rhonda-numbers/Raku/rhonda-numbers.raku
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
use Prime::Factor;
|
||||
|
||||
my @factor-sum;
|
||||
|
||||
@factor-sum[1000000] = 42; # Sink a large index to make access thread safe
|
||||
|
||||
sub rhonda ($base) {
|
||||
(1..∞).hyper.map: { $_ if $base * (@factor-sum[$_] //= .&prime-factors.sum) == [×] .polymod($base xx *) }
|
||||
}
|
||||
|
||||
for (flat 2..16, 17..36).grep: { !.&is-prime } -> $b {
|
||||
put "\nFirst 15 Rhonda numbers to base $b:";
|
||||
my @rhonda = rhonda($b)[^15];
|
||||
my $ch = @rhonda[*-1].chars max @rhonda[*-1].base($b).chars;
|
||||
put "In base 10: " ~ @rhonda».fmt("%{$ch}s").join: ', ';
|
||||
put $b.fmt("In base %2d: ") ~ @rhonda».base($b)».fmt("%{$ch}s").join: ', ';
|
||||
}
|
||||
79
Task/Rhonda-numbers/Rust/rhonda-numbers.rust
Normal file
79
Task/Rhonda-numbers/Rust/rhonda-numbers.rust
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
// [dependencies]
|
||||
// radix_fmt = "1.0"
|
||||
|
||||
fn digit_product(base: u32, mut n: u32) -> u32 {
|
||||
let mut product = 1;
|
||||
while n != 0 {
|
||||
product *= n % base;
|
||||
n /= base;
|
||||
}
|
||||
product
|
||||
}
|
||||
|
||||
fn prime_factor_sum(mut n: u32) -> u32 {
|
||||
let mut sum = 0;
|
||||
while (n & 1) == 0 {
|
||||
sum += 2;
|
||||
n >>= 1;
|
||||
}
|
||||
let mut p = 3;
|
||||
while p * p <= n {
|
||||
while n % p == 0 {
|
||||
sum += p;
|
||||
n /= p;
|
||||
}
|
||||
p += 2;
|
||||
}
|
||||
if n > 1 {
|
||||
sum += n;
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
fn is_prime(n: u32) -> bool {
|
||||
if n < 2 {
|
||||
return false;
|
||||
}
|
||||
if n % 2 == 0 {
|
||||
return n == 2;
|
||||
}
|
||||
if n % 3 == 0 {
|
||||
return n == 3;
|
||||
}
|
||||
let mut p = 5;
|
||||
while p * p <= n {
|
||||
if n % p == 0 {
|
||||
return false;
|
||||
}
|
||||
p += 2;
|
||||
if n % p == 0 {
|
||||
return false;
|
||||
}
|
||||
p += 4;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn is_rhonda(base: u32, n: u32) -> bool {
|
||||
digit_product(base, n) == base * prime_factor_sum(n)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let limit = 15;
|
||||
for base in 2..=36 {
|
||||
if is_prime(base) {
|
||||
continue;
|
||||
}
|
||||
println!("First {} Rhonda numbers to base {}:", limit, base);
|
||||
let numbers: Vec<u32> = (1..).filter(|x| is_rhonda(base, *x)).take(limit).collect();
|
||||
print!("In base 10:");
|
||||
for n in &numbers {
|
||||
print!(" {}", n);
|
||||
}
|
||||
print!("\nIn base {}:", base);
|
||||
for n in &numbers {
|
||||
print!(" {}", radix_fmt::radix(*n, base as u8));
|
||||
}
|
||||
print!("\n\n");
|
||||
}
|
||||
}
|
||||
10
Task/Rhonda-numbers/Sidef/rhonda-numbers.sidef
Normal file
10
Task/Rhonda-numbers/Sidef/rhonda-numbers.sidef
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
func is_rhonda_number(n, base = 10) {
|
||||
base.is_composite || return false
|
||||
n > 0 || return false
|
||||
n.digits(base).prod == base*n.factor.sum
|
||||
}
|
||||
|
||||
for b in (2..16 -> grep { .is_composite }) {
|
||||
say ("First 10 Rhonda numbers to base #{b}: ",
|
||||
10.by { is_rhonda_number(_, b) })
|
||||
}
|
||||
76
Task/Rhonda-numbers/Swift/rhonda-numbers.swift
Normal file
76
Task/Rhonda-numbers/Swift/rhonda-numbers.swift
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
func digitProduct(base: Int, num: Int) -> Int {
|
||||
var product = 1
|
||||
var n = num
|
||||
while n != 0 {
|
||||
product *= n % base
|
||||
n /= base
|
||||
}
|
||||
return product
|
||||
}
|
||||
|
||||
func primeFactorSum(_ num: Int) -> Int {
|
||||
var sum = 0
|
||||
var n = num
|
||||
while (n & 1) == 0 {
|
||||
sum += 2
|
||||
n >>= 1
|
||||
}
|
||||
var p = 3
|
||||
while p * p <= n {
|
||||
while n % p == 0 {
|
||||
sum += p
|
||||
n /= p
|
||||
}
|
||||
p += 2
|
||||
}
|
||||
if n > 1 {
|
||||
sum += n
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func isPrime(_ n: Int) -> Bool {
|
||||
if n < 2 {
|
||||
return false
|
||||
}
|
||||
if n % 2 == 0 {
|
||||
return n == 2
|
||||
}
|
||||
if n % 3 == 0 {
|
||||
return n == 3
|
||||
}
|
||||
var p = 5
|
||||
while p * p <= n {
|
||||
if n % p == 0 {
|
||||
return false
|
||||
}
|
||||
p += 2
|
||||
if n % p == 0 {
|
||||
return false
|
||||
}
|
||||
p += 4
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isRhonda(base: Int, num: Int) -> Bool {
|
||||
return digitProduct(base: base, num: num) == base * primeFactorSum(num)
|
||||
}
|
||||
|
||||
let limit = 15
|
||||
for base in 2...36 {
|
||||
if isPrime(base) {
|
||||
continue
|
||||
}
|
||||
print("First \(limit) Rhonda numbers to base \(base):")
|
||||
let numbers = Array((1...).lazy.filter{ isRhonda(base: base, num: $0) }.prefix(limit))
|
||||
print("In base 10:", terminator: "")
|
||||
for n in numbers {
|
||||
print(" \(n)", terminator: "")
|
||||
}
|
||||
print("\nIn base \(base):", terminator: "")
|
||||
for n in numbers {
|
||||
print(" \(String(n, radix: base))", terminator: "")
|
||||
}
|
||||
print("\n")
|
||||
}
|
||||
33
Task/Rhonda-numbers/Wren/rhonda-numbers.wren
Normal file
33
Task/Rhonda-numbers/Wren/rhonda-numbers.wren
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import "./math" for Math, Int, Nums
|
||||
import "./fmt" for Fmt, Conv
|
||||
|
||||
for (b in 2..36) {
|
||||
if (Int.isPrime(b)) continue
|
||||
var count = 0
|
||||
var rhonda = []
|
||||
var n = 1
|
||||
while (count < 15) {
|
||||
var digits = Int.digits(n, b)
|
||||
if (!digits.contains(0)) {
|
||||
if (b != 10 || (digits.contains(5) && digits.any { |d| d % 2 == 0 })) {
|
||||
var calc1 = Nums.prod(digits)
|
||||
var calc2 = b * Nums.sum(Int.primeFactors(n))
|
||||
if (calc1 == calc2) {
|
||||
rhonda.add(n)
|
||||
count = count + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
n = n + 1
|
||||
}
|
||||
if (rhonda.count > 0) {
|
||||
System.print("\nFirst 15 Rhonda numbers in base %(b):")
|
||||
var rhonda2 = rhonda.map { |r| r.toString }.toList
|
||||
var rhonda3 = rhonda.map { |r| Conv.Itoa(r, b) }.toList
|
||||
var maxLen2 = Nums.max(rhonda2.map { |r| r.count })
|
||||
var maxLen3 = Nums.max(rhonda3.map { |r| r.count })
|
||||
var maxLen = Math.max(maxLen2, maxLen3) + 1
|
||||
Fmt.print("In base 10: $*s", maxLen, rhonda2)
|
||||
Fmt.print("In base $-2d: $*s", b, maxLen, rhonda3)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue