Squares and Cubes

[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

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:

bullet Dynamic arrays - the size is defined at run time and can change as required.
bulletSorted array - a simple insertion techniques is used to shift up all values higher than the value being inserted so that the array is maintained in increasing sequence by sum.
bulletFunction passed as a parameter - the 50 lines of code required to find the solution  for squares is identical for the cubes case except for a couple of places where we must cube the number instead of squaring it.  By making Squareit and Cubeit functions and passing them as a parameter to the DoCalc function, the code is perfectly sharable,     

#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 of digits.
So here's the plan: We'll start by checking all 4 digit values that have different 4 different digits in an outer loop. We'll cube it and compare that number to the squared valued of all possible integers made up of the 6 remaining digits. The Combo unit uses concepts introduced in Permutes and Alphametics and will be used to get all 720 permutations of the 6 digits.  If the squared value of one of them is equal to the cubed value of the 4 digit number. we're done. Since Combo.Getnext returns permutations in lexicographic order, the numbers generated will be in increasing vale and if the squared value exceeds the cubed value, we can break out the inner loop and go on the next 4 digit number.

Running/Exploring the Program 

bulletBrowse source extract
bullet#1
bullet#2
bullet#3
bullet#4
bullet#5
bulletDownload source
bulletDownload  executable - not uploaded since size for all 5 would be large and the answers aren't very important anyway.  In this case, the value is in the process - not the product.   

Suggestions for Further Explorations

For problem #1, the sum of consecutive cubes is a square, the speed could be greatly enhanced by putting the cubes of integers into a table first and then using a lookup instead of recalculating cubes each time.   Since the highest start value is 20 and the most consecutive cubes to be summed is 100, we could just calculate the first 120 (or maybe 121) cubes in and array before the loop starts.  
A number of these could do a better job of explaining the results, for example #4, the Pythagorean triples should probably also display the area and perimeter values.
 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.