• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
A puzzle
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: A puzzle
      • From: Luther Fuller <email@hidden>
    • Re: A puzzle
      • From: Luther Fuller <email@hidden>
    • Re: A puzzle
      • From: Luther Fuller <email@hidden>
    • Re: A puzzle
      • From: Paul Berkowitz <email@hidden>
    • Re: A puzzle
      • From: Deivy Petrescu <email@hidden>
    • Re: A puzzle
      • From: Simon Topliss <email@hidden>
    • Re: A puzzle
      • From: Ed Stockly <email@hidden>
    • Re: A puzzle
      • From: "Mark J. Reed" <email@hidden>
  • Prev by Date: Scripting Apple Mail to Send Again
  • Next by Date: Re: A puzzle
  • Previous by thread: Re: Scripting Apple Mail to Send Again
  • Next by thread: Re: A puzzle
  • Index(es):
    • Date
    • Thread