[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Problem DescriptionDemonstrate techniques for extracting decimal digits from integer numbers for further manipulation. Background & TechniquesMany 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}
Suggestions for Further Explorations
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |