Re: A puzzle
Re: A puzzle
- Subject: Re: A puzzle
- From: Deivy Petrescu <email@hidden>
- Date: Fri, 21 Nov 2008 12:27:09 -0500
On Nov 21, 2008, at 11/21 12:17 , Luther Fuller wrote: On Nov 21, 2008, at 9:59 AM, Michelle Steiner 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:
...
It took 63 seconds on my machine.
This cut it down to 58 seconds:
...
They're both brute force, and I have a gut feeling that there's a more elegant method that I'm overlooking.
And this cuts it down to 30 sec ... set startTime to (current date) set AppleScript's text item delimiters to {"1"} set counter to 0 repeat with i from 0 to 999999 set counter to (counter + (count (text items of (i as text))) - 1) end repeat (current date) - startTime display dialog (counter as text) & return & the result & " sec"
I think you are a mathematician, we think alike... :)
I came up with the following script:
______ set a to chrono --- uses Smile script k property padding : "000000" property tid : "1" property counter : 0 end script set AppleScript's text item delimiters to k's tid repeat with i from 0 to 999999 set x to characters -6 thru -1 of (k's padding) & i set (k's counter) to (k's counter) + (count text items of x) - 1 end repeat {chrono, k's counter} ____
I wanted something that would keep the symmetry of the problem. that is you can change "1" by any other digit, including 0.
|
_______________________________________________
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>) |
| >Re: A puzzle (From: Luther Fuller <email@hidden>) |