Re: A puzzle
Re: A puzzle
- Subject: Re: A puzzle
- From: Paul Berkowitz <email@hidden>
- Date: Fri, 21 Nov 2008 08:54:34 -0800
- Thread-topic: A puzzle
Title: Re: A puzzle
On 11/21/08 7:59 AM, "Michelle Steiner" <email@hidden> wrote:
Your car odometer has 6 digits, from 000000 to 999999.
If your car made it to 1,000,000 miles, how many times would the
number 1 have shown up?
My first Applescript solution was:
set counter to 0
repeat with i from 0 to 999999
set x to i as string
if "1" is in x then
repeat with j in x
if contents of j is "1" then
set counter to counter + 1
end if
end repeat
end if
end repeat
counter
It took 63 seconds on my machine.
This cut it down to 58 seconds:
set counter to 0
repeat with i from 0 to 999999
set x to i as string
if "1" is in x then
repeat with j from 1 to length of x
if character j of x is "1" then
set counter to counter + 1
end if
end repeat
end if
end repeat
counter
They're both brute force, and I have a gut feeling that there's a more elegant method that I'm overlooking.
-- Michelle
Here's the method given on Car Talk a few weeks ago:
There are 10 digits (0-9).
All of them occur an equal number of times.
So the number 1 shows up (1,000,000 / 10) times
= 100,000
QED
--
Paul Berkowitz
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
References: | |
| >A puzzle (From: Michelle Steiner <email@hidden>) |