Digits and Numbers

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

Contact

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

Search DelphiForFun.org only

 

 

Problem Description

Demonstrate techniques for extracting decimal digits from integer numbers for further manipulation.

Background & Techniques

Many integer arithmetic problems and puzzles require that the individual decimal digits be separated from or combined into multi-digit numbers.

Here are program solutions for a few problems adapted from the latest Dover Publications addition to my library,  Challenging Mathematical Teasers (Dover Recreational Math)  by J.A.H. Hunter. 

The book has 100 math problems which really I found really challenging to solve without the help of my trusty laptop and Delphi compiler.  Solutions with explanations are included if you want to sharpen your analytical skills but using the computer is my way (the lazy man's way J).

The puzzles I selected all involve extracting digits from numbers which is easy once you see how it's done.  Decimal number systems are almost universal in modern civilizations, probably because of the evolution provided most of us with 10 fingers. Western civilizations use the positional system  developed by Indian mathematicians about 2000 years ago. In that system successive powers of 10 are represented by their positions from right to left. One of the consequences is that we need something to represent the absence of any power of 10 in that position, thus "zero" was born.

Western countries use the 10 symbols '0' through '9' so, for example the number 1023 represents a quantity equal to 1000 things plus 20 things plus 3 things. 

Only two operations are required to extract the digits from a number within a program. "a Div b" is integer division and returns the whole number quotient, dropping any remainder, and "a Mod b", the modulus function, which returns the remainder when "a" is divided by "b". So "1023 mod 10" returns 3 effectively dropping all except the low order digit. Also "1023 div 10" returns 102, in effect shifting the number one position to the right and/or dropping the low order digit, whichever way you prefer to think about it. Repeating the "mod" and "div" operations in a similar manner "102", then on "10" then on "1" can extract all of the digits into separate variable fields. 

As an example, given the problem: "Find the 3 digit numbers which are 35 times the sum of their digits.", The essential Delphi solution code looks like this (Comments are in red and enclosed in curly brackets {}):

for n:=100 to 999 do {Loop for all 3 digit numbers}
begin
  test:=n; {Make "test" the work field}
  sum:=test mod 10; {Put the low order digit of "test" into "sum"}
 
test:=test div 10; {shift "test" one digit to the right}
 
sum:=sum + test mod 10; {add the second digit to sum}
  test:=test div 10; {get down to the final digit}
  sum:=sum+ test; {add it to "sum"}
  {is n an exact multiple of sum and does n div sum =35?}
  if (n mod sum = 0) and (n div sum =35)
  then showmessage( 'The number '+inttostr(n) +' is '
                                + ' 35 times the sum of its digits!');
end;


Running/Exploring the Program 

Suggestions for Further Explorations

 ???
   

 

Original:  October 16, 2012

Modified:  February 18, 2016

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