in reply to Re: Re: brainteaser: splitting up a namespace evenly
in thread brainteaser: splitting up a namespace evenly

OK, you'll have to give us a bit more information about the set of numbers you're trying to hash...... Tough to come up with a clever way to spread them out when we don't know much about the set. ;-)

-Blake

  • Comment on Re: Re: Re: brainteaser: splitting up a namespace evenly

Replies are listed 'Best First'.
Re: Re: Re: Re: brainteaser: splitting up a namespace evenly
by perrin (Chancellor) on Oct 24, 2001 at 04:00 UTC
    To solve the brainteaser, the solution should be able to work on any set of 10000 randomly generated nine digit numbers. Anything short of that is just practical, and I'm in this for the fun of it at this point. :)
      Well, here is a quick hack comparing two hashing algorithms. The first keys on the sum of the digits (1234 -> 10), The second uses the last two chars (1234 -> 34). Since we started with a random set, the results aren't too surprising.....
      #!/usr/bin/perl -wT use strict; my $count = shift || 10000; my $verbose = shift || 0; my %hash; for (1..$count) { my $value = int(rand(899_999_999)+100_000_000); # uncomment one of the next two lines to toggle algorithms #my $hv = digitsum($value); my $hv = lastchars($value); print "$value => $hv\n" if $verbose; $hash{$hv}++; } my $keycount = keys %hash; my @orderedvalues = (sort {$a <=> $b} values %hash); my ($min,$max) = (@orderedvalues)[0,-1]; print "$keycount keys\n"; print "Min:$min\n"; print "Max:$max\n"; for (@orderedvalues) { printf "%4d:",$_; print '*' x int($_/$max*70),"\n"; } sub digitsum { my $key = shift; my $hv; $hv += $_ for (split//,$key); return $hv; } sub lastchars { my $key = shift; my ($hv) = ($key =~ /(..)$/); return $hv; } =OUTPUT using lastchars 100 keys Min:77 Max:119 77:********************************************* 79:********************************************** 80:*********************************************** 80:*********************************************** 81:*********************************************** 82:************************************************ 85:************************************************** 86:************************************************** 86:************************************************** 86:************************************************** 87:*************************************************** 87:*************************************************** 88:*************************************************** 88:*************************************************** 88:*************************************************** 88:*************************************************** 88:*************************************************** 88:*************************************************** 88:*************************************************** 89:**************************************************** 89:**************************************************** 89:**************************************************** 90:**************************************************** 90:**************************************************** 91:***************************************************** 91:***************************************************** 92:****************************************************** 93:****************************************************** 93:****************************************************** 93:****************************************************** 93:****************************************************** 94:******************************************************* 94:******************************************************* 95:******************************************************* 95:******************************************************* 95:******************************************************* 96:******************************************************** 96:******************************************************** 97:********************************************************* 97:********************************************************* 97:********************************************************* 97:********************************************************* 98:********************************************************* 98:********************************************************* 98:********************************************************* 99:********************************************************** 99:********************************************************** 99:********************************************************** 100:********************************************************** 100:********************************************************** 100:********************************************************** 100:********************************************************** 101:*********************************************************** 102:************************************************************ 102:************************************************************ 102:************************************************************ 103:************************************************************ 103:************************************************************ 103:************************************************************ 104:************************************************************* 105:************************************************************* 105:************************************************************* 106:************************************************************** 106:************************************************************** 106:************************************************************** 106:************************************************************** 106:************************************************************** 106:************************************************************** 107:************************************************************** 107:************************************************************** 107:************************************************************** 107:************************************************************** 108:*************************************************************** 109:**************************************************************** 109:**************************************************************** 109:**************************************************************** 109:**************************************************************** 109:**************************************************************** 110:**************************************************************** 110:**************************************************************** 110:**************************************************************** 110:**************************************************************** 111:***************************************************************** 111:***************************************************************** 112:***************************************************************** 112:***************************************************************** 113:***************************************************************** +* 113:***************************************************************** +* 114:***************************************************************** +** 114:***************************************************************** +** 114:***************************************************************** +** 115:***************************************************************** +** 116:***************************************************************** +*** 116:***************************************************************** +*** 116:***************************************************************** +*** 116:***************************************************************** +*** 116:***************************************************************** +*** 118:***************************************************************** +**** 118:***************************************************************** +**** 119:***************************************************************** +***** =OUTPUT using digitsum 59 keys Min:1 Max:477 1: 1: 1: 2: 2: 2: 4: 4: 5: 5: 6: 10:* 12:* 12:* 16:** 19:** 21:*** 22:*** 25:*** 29:**** 35:***** 45:****** 51:******* 51:******* 63:********* 68:********* 74:********** 88:************ 96:************** 113:**************** 127:****************** 138:******************** 145:********************* 152:********************** 172:************************* 184:*************************** 209:****************************** 225:********************************* 226:********************************* 242:*********************************** 249:************************************ 266:*************************************** 283:***************************************** 294:******************************************* 309:********************************************* 341:************************************************** 364:***************************************************** 380:******************************************************* 381:******************************************************* 383:******************************************************** 422:************************************************************* 424:************************************************************** 439:**************************************************************** 441:**************************************************************** 446:***************************************************************** 458:***************************************************************** +** 464:***************************************************************** +*** 476:***************************************************************** +**** 477:***************************************************************** +*****

      -Blake