Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -0,0 +1,25 @@
# syntax: GAWK -f DIGITAL_ROOT.AWK
BEGIN {
n = split("627615,39390,588225,393900588225,10,199",arr,",")
for (i=1; i<=n; i++) {
dr = digitalroot(arr[i],10)
printf("%12.0f has additive persistence %d and digital root of %d\n",arr[i],p,dr)
}
exit(0)
}
function digitalroot(n,b) {
p = 0 # global
while (n >= b) {
p++
n = digitsum(n,b)
}
return(n)
}
function digitsum(n,b, q,s) {
while (n != 0) {
q = int(n / b)
s += n - q * b
n = q
}
return(s)
}

View file

@ -0,0 +1,29 @@
( root
= sum persistence n d
. !arg:(~>9.?)
| !arg:(?n.?persistence)
& 0:?sum
& ( @( !n
: ?
(#%@?d&!d+!sum:?sum&~)
?
)
| root$(!sum.!persistence+1)
)
)
& ( 627615 39390 588225 393900588225 10 199
: ?
( #%@?N
& root$(!N.0):(?Sum.?Persistence)
& out
$ ( !N
"has additive persistence"
!Persistence
"and digital root of"
!Sum
)
& ~
)
?
| done
);

View file

@ -1,12 +1,13 @@
import std.stdio, std.typecons, std.conv, std.bigint;
import std.stdio, std.typecons, std.conv, std.bigint, std.math,
std.traits;
Tuple!(int, T) digitalRoot(T)(T root, in uint base) /*pure nothrow*/
Tuple!(uint, Unqual!T) digitalRoot(T)(in T inRoot, in uint base)
pure /*nothrow*/
in {
assert(base > 1);
} body {
if (root < 0)
root = -root;
int persistence = 0;
Unqual!T root = inRoot.abs;
uint persistence = 0;
while (root >= base) {
auto num = root;
root = 0;
@ -16,22 +17,20 @@ in {
}
persistence++;
}
return tuple(persistence, root);
return typeof(return)(persistence, root);
}
void main() {
// import std.stdio, std.conv, std.bigint;
enum f1 = "%s(%d): additive persistance= %d, digital root= %d";
foreach (b; [2, 3, 8, 10, 16, 36]) {
foreach (n; [5, 627615, 39390, 588225, 393900588225])
writefln(f1, to!string(n,b), b, digitalRoot(n, b).tupleof);
writeln();
foreach (immutable b; [2, 3, 8, 10, 16, 36]) {
foreach (immutable n; [5, 627615, 39390, 588225, 393900588225])
writefln(f1, text(n, b), b, n.digitalRoot(b)[]);
writeln;
}
enum f2 = "<BIG>(%d): additive persistance= %d, digital root= %d";
foreach (b; [2, 3, 8, 10, 16, 36]) {
auto n = BigInt("58142718981673030403681039458302204471" ~
"300738980834668522257090844071443085937");
writefln(f2, b, digitalRoot(n, b).tupleof); // shortened output
}
immutable n = BigInt("581427189816730304036810394583022044713" ~
"00738980834668522257090844071443085937");
foreach (immutable b; [2, 3, 8, 10, 16, 36])
writefln(f2, b, n.digitalRoot(b)[]); // Shortened output.
}

View file

@ -0,0 +1,15 @@
-module( digital_root ).
-export( [task/0] ).
task() ->
Ns = [N || N <- [627615, 39390, 588225, 393900588225]],
Persistances = [persistance_root(X) || X <- Ns],
[io:fwrite("~p has additive persistence ~p and digital root of ~p~n", [X, Y, Z]) || {X, {Y, Z}} <- lists:zip(Ns, Persistances)].
persistance_root( X ) -> persistance_root( sum_digits:sum_digits(X), 1 ).
persistance_root( X, N ) when X < 10 -> {N, X};
persistance_root( X, N ) -> persistance_root( sum_digits:sum_digits(X), N + 1 ).

View file

@ -0,0 +1,2 @@
: (Sdigit) 0 swap begin base @ /mod >r + r> dup 0= until drop ;
: digiroot 0 swap begin (Sdigit) >r 1+ r> dup base @ < until ;

View file

@ -0,0 +1,4 @@
[UNDEFINED] mu/mod [IF] : mu/mod >r 0 r@ um/mod r> swap >r um/mod r> ; [THEN]
: (Sdigit) 0. 2swap begin base @ mu/mod 2>r s>d d+ 2r> 2dup d0= until 2drop ;
: digiroot 0 -rot begin (Sdigit) 2>r 1+ 2r> 2dup base @ s>d d< until d>s ;

View file

@ -0,0 +1,51 @@
#!perl
use strict;
use warnings;
use List::Util qw(sum);
my @digit = (0..9, 'a'..'z');
my %digit = map { +$digit[$_], $_ } 0 .. $#digit;
sub base {
my ($n, $b) = @_;
$b ||= 10;
die if $b > @digit;
my $result = '';
while( $n ) {
$result .= $digit[ $n % $b ];
$n = int( $n / $b );
}
reverse($result) || '0';
}
sub digi_root {
my ($n, $b) = @_;
my $inbase = base($n, $b);
my $additive_persistance = 0;
while( length($inbase) > 1 ) {
++$additive_persistance;
$n = sum @digit{split //, $inbase};
$inbase = base($n, $b);
}
$additive_persistance, $n;
}
MAIN: {
my @numbers = (5, 627615, 39390, 588225, 393900588225);
my @bases = (2, 3, 8, 10, 16, 36);
my $fmt = "%25s(%2s): persistance = %s, root = %2s\n";
if( eval { require Math::BigInt; 1 } ) {
push @numbers, Math::BigInt->new("5814271898167303040368".
"1039458302204471300738980834668522257090844071443085937");
}
for my $base (@bases) {
for my $num (@numbers) {
my $inbase = base($num, $base);
$inbase = 'BIG' if length($inbase) > 25;
printf $fmt, $inbase, $base, digi_root($num, $base);
}
print "\n";
}
}

View file

@ -1,12 +1,12 @@
class String
def digroot_persistance(base)
def digroot_persistence(base)
num = self.to_i(base)
persistance = 0
persistence = 0
until num < base do
num = num.to_s(base).each_char.reduce(0){|m, c| m += c.to_i(base) }
persistance += 1
persistence += 1
end
[num.to_s(base), persistance]
[num.to_s(base), persistence]
end
end
@ -16,4 +16,4 @@ end
["5", 10],
["393900588225", 10],
["50YE8N29", 36]].each{|(str, base)| puts "#{str} base #{base} has a digital root \
of %s and a resistance of %s." % str.digroot_persistance(base) }
of %s and a persistence of %s." % str.digroot_persistence(base) }