why 31 in hashcode for string

J

jimgardener

what is the reason for using 31 in the calculation of hashcode for
String?can anyone give a precise answer?
thanks
jim
 
R

Roedy Green

what is the reason for using 31 in the calculation of hashcode for
String?can anyone give a precise answer?

Is is a prime. the extreme opposite would use a power of two, which
would just shift, throwing away variability. Compound numbers are
somewhere between.

A little math puzzle for you.:

what is the next number in this series?

2, 6, 30, 210, 2310, ...
 
R

Roedy Green

The prime hint makes it trivial.

How would you go about writing a computer program to solves such
problems? People have no problem seeing new patterns.
 
D

Daniel Pitts

Kenneth said:
Try this one..

Write a program to find all the numbers where their value is equal to the
sum of the digits each raised to the power of the number of significant
digits.

Examples:

1, 2, 3, .... 153, 370, 371,

I've always heard this called the Armstrong set, but apparently there are
different definitions for this set in common use.

It has a finite number of members. I wrote a program a few years ago to
enumerate them all in common lisp. Brute force won't do it.

Good luck.
Well, I can easily and compactly tell you all the 2 digit ones:
<>
:)
 
A

Andy Dingley

How would you go about writing a computer program to solves such
problems? People have no problem seeing new patterns.

In general, the same way Victorians evaluated polynomials with their
steam computers - the method of successive differences. Calculate the
differences between successive terms of a series, then the differences
between successive differences, and pretty soon a "subtle" pattern
will throw up an "obvious" pattern in its diffs.

Of course this fails dismally for primes! In that case, you'd compare
the differences to a set of "well known sequences", primes being an
obvious candidate, along with the birthdates of US Presidents, run
lengths of Startrek movies, etc. according to the relevant fandom of
the cryptographer setting the puzzle.
 
T

Tom Anderson

In general, the same way Victorians evaluated polynomials with their
steam computers - the method of successive differences.

'The Victorian', surely - was there anyone other than Charlie the Cabbage?
Calculate the differences between successive terms of a series, then the
differences between successive differences, and pretty soon a "subtle"
pattern will throw up an "obvious" pattern in its diffs.

Of course this fails dismally for primes! In that case, you'd compare
the differences to a set of "well known sequences", primes being an
obvious candidate, along with the birthdates of US Presidents, run
lengths of Startrek movies, etc. according to the relevant fandom of the
cryptographer setting the puzzle.

For the lazy and/or smart, searchy searchy:

http://www.research.att.com/~njas/sequences/

tom
 
T

Tom Anderson

Several! Not usually English or American though, so they tended to fall
out of the histories.

If you visit the Science Museum, they have a working reconstruction of a
Babbage Difference Engine. In the case opposite, is a period difference
engine, almost a contemporary and a working machine that was used in its
day. Of course its maker (a Swede, AFAIR) just made a difference engine
and got on with it, he didn't spend most of his time trying to either
re-design and improve it, or else dreaming of an Analytical Engine
instead.

Aha. I've been in that room numerous times, but i confess my attention is
usually consumed by the Babbage engine! Oh, and the harmonic analyser,
too, which is very clever. I will pay more attention to the other machines
next time.

tom
 
A

Andy Dingley

Oh, and the harmonic analyser, too, which is very clever.

The sphere and disk machine?

I spent a whole day in there recently, the first time I'd ever been to
the Science Museum and not had a deadline or timescale to keep up
with. I only left when I ran out of camera batteries. The wikipedia
writing backlog (including the sphere and disk) is slowly getting
worked through...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top