In this QA, https://scifi.stackexchange.com/q/311/3804, which says that Adams got his number from good old-fashioned garden-gazing.
But while I was looking for the sum completed by the apes in Hitchhiker's Guide to the Galaxy I came across something peculiar, Lewis Carroll seems to make more references to 42 than Adams:
- Alice's Adventures in Wonderland has 42 illustrations.
- Alice's attempts at multiplication (chapter two of Adventures in Wonderland) work if one uses base 18 to write the first answer, and increases the base by threes to 21, 24, etc. (the answers working up to 4 × 12 = "19" in base 39), but "breaks" precisely when one attempts the answer to 4 × 13 in base 42, leading Alice to declare "oh dear! I shall never get to twenty at that rate!"
- Rule Forty-two in Alice's Adventures in Wonderland ("All persons more than a mile high to leave the court").
- Rule 42 of the Code in the preface to The Hunting of the Snark ("No one shall speak to the Man at the Helm").
- In "fit the first" of The Hunting of the Snark the Baker had "forty-two boxes, all carefully packed, With his name painted clearly on each."
- The White Queen announces her age as "one hundred and one, five months and a day", which—if the best[clarification needed] possible date is assumed for the action of Through the Looking-Glass—gives a total of 37,044 days. If the Red Queen, as part of the same chess set, is regarded as the same age, their combined age is 74,088 days, or 42 × 42 × 42.
What's the relationship between the two authors? I've not found anything stating Adams was influenced by Carroll, besides the obvious absurd/surreal humour. This question, What were some of Douglas Adams's HHGG's influences?, for instance doesn't mention Carroll at all!