• 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
Re: A puzzle
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A puzzle


  • Subject: Re: A puzzle
  • From: Deivy Marck Petrescu <email@hidden>
  • Date: Fri, 21 Nov 2008 17:18:06 -0500


On Nov 21, 2008, at 16:43, Mark J. Reed wrote:

On Fri, Nov 21, 2008 at 4:19 PM, Luther Fuller <email@hidden> wrote:
But, what if we let d = 0? If we use the brute force script, we find that if
n = 6, then there are 488890 zeros in the print-out. Of course, the answer
is different in this case since leading zeros aren't printed.

Well, in an odometer, at least a traditional mechanical one, the
leading zeroes are there.  But assume it's digital and leading zeroes
don't count.  Then you have to construct a sum.  Start with the same
total (n * 10^(n-1)).

1. Subtract any zeroes that appear in the high digit; there's one of
those for every possible value of the remaining digits, so that's
10^(n-1).
2. Subtract any zeroes in the second-to-highest digit that occur when
the high digit is zero: that's 10^(n-2).

Etc.  Continue counting down and subtracting.

600000 - 100000 - 10000 - 1000 - 100 - 10 - 1 = 488889.

but then you have to add 1 back for the case where the total value is
0, even though that's technically a "leading" zero.

488889 + 1 = 488890.

So the general result is  n * 10^(n-1) - [sum from 1 to n of 10^n] +
1.  Of course, that middle number is just a sequence of '1's of length
n, easily written down directly when you know n:

600000 - 111111 + 1 = 488890.







--
Mark J. Reed <email@hidden>

Good work Mark!
I came up with a different approach taking away the front zeros.
So general formula would be

600000-9*(6-1)-90*(6-2)-900*(6-3)-9000*(6-4)-90000*(6-5)-1*(6-1)

But yours is much better.  
You can write a general formula for any number of digits.

Here:

___
repeat
display dialog "How many digits does your odometer have?" default answer ""
try
set n to (text returned of the result) as integer
exit repeat
end try
end repeat
set quant to (n * (10 ^ (n - 1)))
set less0 to (10 ^ n - 10) / 9
display dialog "The digits from 1 to 9 will show up " & quant & " times and the 0 digit would show up " & (quant - less0) & " times. " buttons {"Cancel"} default button 1
____


Deivy
 _______________________________________________
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>)
 >Re: A puzzle (From: "Mark J. Reed" <email@hidden>)

  • Prev by Date: Re: A puzzle
  • Next by Date: Re: A puzzle
  • Previous by thread: Re: A puzzle
  • Next by thread: Re: A puzzle
  • Index(es):
    • Date
    • Thread