Most All 3

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]



Search WWW


As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search only



Problem Description

Almost every integer contains at least one occurrence of the digit 3!  Prove this for yourself by counting how many contain a 3 for numbers up to 10, up to 100, 1000, 10,000, etc.   

Background and Techniques

I put 2 buttons on this program form called "The Dumb Way" and "The Smart Way" .  "The Dumb Way" was to start counting  inside a loop, converting each integer to a string, scanning the strings for 3's, and adding to a counter when the first 3 was found in each number.  

The smart way uses the following  analysis:  for integers up to 10 there is one number containing a 3 (10%),   up to 100 there are 9 of these "sets of 10", (0-9, 10-19, 20-29, 40-49, etc.) each containing a single 3, and one "set of 10" where each number has a 3 (30-39) for a total of 19 (19%).  Up to 1000 there are nine sets of 100 each containing 19 "3's"   and one set containing 100 ( the "300's") for a total of 271 (27.1%).  Are you starting to see a pattern here?   (Try 10n-9n.)

The other Delphi thing you might learn from this program is he technique used here to arrange the display data into columns.   There is  a TListView component that supports columns, but in this case I wanted to put new column headings in the middle of the display for the :"Smart way" button.   The algorithm used is to divide the listbox width by 4 and right align the data in each of the first 3.  The routine   AdjustListBoxText does this inserting spaces at the beginning  of the each string until  canvas.textwidth is the desired width.   

 Running/Exploring the Program


Browse source code extract.


Download source.


Download executable.

Suggestions for further exploration

Not much Delphi-wise, but if you're interested in large numbers...

The idea of infinity is not an easy one to grasp.  You can start to get an idea if you consider the claim that almost all integers contain, not just a "3", but all of the digits from "0" through "9" at least once.  In fact most integers contain the sequence of digits "0123456789" at least once.  In fact, most integers contain  "0123456789" at least a thousand times!     

You've may have heard  Bob Newhart's comedy skit about putting enough monkeys in front of  typewriters and letting them work long enough to produce the complete works of Wm. Shakespeare.     Since the number of rearrangements of the letters in Shakespeare's works is finite, the idea is true (in theory).  If we assume Shakespeare's works contain 100,000 words and each word averages 5 characters then we have half a million character positions to fill.  Let's  assume that there are 35 choices for each   - oh heck let's say 100 choices, we want the capitalization to be correct plus the numbers plus punctuation marks and spaces.    So we have only 100500,000 arrangements to try.  Maybe only half that many before we hit up the correct one.  This is a number between a google and a googleplex.   A mere drop in the bucket in the world of large numbers.  

Try  searches on infinity  and googolplex  to learn more about big.   You can also search on "monkeys Shakespeare typewriters" and get at least one essay calculating the time required.   (And a bunch of jokes that read like this: "We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true.")

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.