[Home] [Puzzles & Projects] [Delphi Techniques] [Math Topics] [Library] [Utilities]
Here are 5 little programs that all involve the squares and cubes of integers.
#1: The sum of the cubes of any number of consecutive integers starting with 1 is the square of some integer. (For example 13+23=9=32, 13+23+33=36=62, etc.) How about finding the first sum of four or more consecutive cubes (not starting with 1) that is the square of an integer?
#2: Find the smallest integer that can be formed as the sum of two squares in two different ways. (i.e. X = a2+b2 = c2+d2 where a, b, c, and d are all different). Too, easy? Then find the smallest integer that can be formed as the sum of two cubes in two different ways.
#3: There is a number which, when cubed is 2,000,000 larger than a number which is the square of a factor of 2,000,000. What is the number?
#4: A Pythagorean triangle is a right-angle triangle whose sides are all integer values. The area of a Pythagorean triangle (or any other right triangle) is 1/2 the product of the two shortest sides. The perimeter of any triangle is the sum of the 3 sides. A number whose square root is an integer is called a perfect square and a number whose cube root is an integer is called a perfect cube. Find the smallest Pythagorean triangle whose perimeter is a perfect square and whose area is a perfect cube.
#5: Finally the toughest one, find two integers with the following properties: The square of the first equals the cube of the second and together they contain all of the digits 0 to 9 exactly once.
Background & Techniques
#1: The solution implemented is straightforward. Starting with 2, we'll test each integer to see if it could be the start of our series (sum of consecutive cubes equals a perfect square) until we solve it or reach some maximum value. For each start value we'll embed a second loop adding up consecutive cubes and looking for a square. I arbitrarily limited the size of this loop to 100 iterations which turns out to be plenty.
#2: (Smallest integer that is sum of 2 squares in 2 ways). Also straightforward, but slightly more complicated. We use a dynamic array of TSumRec records to save trial sums in increasing sequence as we calculate them. TSumRec contains the the two numbers and the sum of squares so that we can easily check all past sums against the current sum - if we find a match then we have found two ways to produce the sum and we are finished. Three topics that might be new to the novice programmer here are:
#3: Getting the following equation from the problem description was probably the hardest part of this problem. Mathematically the problem condition is to find n such that 2,000,000 mod sqrt( n3-2,000,000) =0 where n3 -2,000,000 is a perfect square. (Mod is the modulo operator, the remainder when the first operand is divided by the second. If a mod b = 0 then b is a factor of a by definition. ) So now, all we have to do is to pick a starting value for n and try values from there on up until we find one that satisfies the equation.
#4: OK, on to Pythagoras. I think that there are some formulas for generating Pythagorean triples, sets of integers with the property that a2+b2=c2. But the one I tried didn't seem to work, so I just reverted back to good old trial and error. We'll run a loop incrementing b inside of a loop incrementing a and when sqrt(a2+b2) is an integer, check the area and perimeter for the desired conditions. Two other features that might be of interest to novice programmers is the use of the Format statement to format the displayed result and the use of QueryPerformanceCounter and QueryPerformanceFrequency procedures to accurately determine run times.
#5: The hardest one. ( Find two integers with the
properties that the square of the first equals the cube of the second and together they contain all of the
digits 0 to 9 exactly once.) Fortunately we have the Combo
unit available to help get the permutations needed in solving. Since there are 10 digits in the results, there
must be 4 in the number to be cubed and 6 in the number to be squared.
Using the max and min size of squares and cubes of n digit numbers, you can
convince yourself that this condition must be satisfied. 4 and 6 are the only
2 lengths that add to 10 and whose cubes and squares could have the same number
Running/Exploring the Program
Suggestions for Further Explorations
Copyright © 2000-2014, Gary Darby
All rights reserved.