CarTalk "Ones" Puzzlers

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

 

Search

Search WWW

Search DelphiForFun.org

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.)

Contact

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

Search DelphiForFun.org only

 

 

Problem Description

Two "Puzzlers" from Car Talk, my favorite NPR radio show:

RAY: This is from my higher mathematics series and it's a twist on a puzzler I gave some months ago. The idea for this puzzler was sent in by Tim Davis.

He writes:

"How many times does the mileage on an odometer not contain the number 1 at all?"

For example 999,999 doesn't have it. So the question is, how many times does the mileage appear going from 000000 to all 9's (999,999), with no 1s at all?

To refresh your memories, we had a puzzler last Fall asking how many times the number 1 will appear on the odometer that goes from all zeroes, 000000, to all nines, 999999, once it completely turns over. For example at mile 000111, the number 1 appears three times.
 

Background & Techniques

These puzzlers  do not require a computer program to solve but  exhaustive search solutions are  quite easy to code.  Although the problem asks about a specific digit, "1", and a specific integer length, 6, it is just as easy for a program check any digit on any integer length.  All we have to do is collect the information from the user as input values.

I rephrased the "How many with no occurrences?" question to "How many have exactly N occurrences?" which is an equivalent question when N=0.  

For programmers, two items for your bag of tricks which are worth remembering:

bullet I implemented 2 versions of counting digit occurrences of the second problem just to compare run rimes:
bullet  1). Convert each number to a string and count occurrences of the character version of target digit in the string.
bullet 2).Mathematically count occurrences by the "div and mod" method (even non-programmers can probably read and understand this code  Comments are in red).

While n<>0 do
begin
  digit:=n mod 10;
{get the units digit}
  if digit=search-digit then sum:=sum+1;
{check it}
  n:=n div 10; 
{shift the number right by one digit}
end;

This second version is about 4 times faster than the first.
 
bullet The Format  function is a flexible method for creating formatted strings for output but has no conversion type to convert an integer to a string with commas ("thousands separators" actually since the Europeans insist on exchanging "," and "." characters).   But if we add 0.0 to the integer to be output, Delphi will convert it to floating point and the %.0N format specifier will insert thousands separators. appropriately. 

Running/Exploring the Program 

bullet Download source
bullet Download  executable

Suggestions for Further Explorations

???

 

Original Date: June 14, 2009

Modified: July 29, 2017

 

 

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