When you say "using int arithmetic"; signed or unsigned? 32-bit?

Could you supply the hash value generated for "The quick brown fox jumps over the lazy dog" so that we may have something to check our attempts against.

This might be close, and reasonable efficient as it avoids the exponentiation by processing the string backwards and accumulating the exponent, and uses integer math. However truncating to 32-bits in reverse order maybe screwing up the result?

#! perl -slw use strict; # s[0]*31^(n-1)+s[1]*31^(n-2)+ ... +s[n-1] sub JavaHash { use integer; my ($s) = @_; my $n = length $s; my @s = unpack 'C*', $s; my $hash = 0; my $p = 1; for ( reverse @s ) { print "$hash : $_: $p"; $hash += $_ * $p; $p *= 31; } return $hash; } die 'No arguments' unless @ARGV; printf '%10d:%s' , JavaHash( $_ ), $_ for @ARGV;

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: Generating A Hash Value by BrowserUk
in thread Generating A Hash Value by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.