A puzzle
A puzzle
- Subject: A puzzle
- From: Michelle Steiner <email@hidden>
- Date: Fri, 21 Nov 2008 08:59:03 -0700
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
-- You don't need to be 'straight' to fight for your country. You just need to shoot straight. -- Barry Goldwater
|
_______________________________________________
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