Menu

Problem Write Positive Integer N Sum 4 Squares Mathematicians Prove Positive Integer Writt Q43840064

Problem. Write positive integer n as a sum of at most 4squares.
Mathematicians can prove that a positive integer can be written asthe sum of at most 4 squares. That is, if n is a
positive integer, then there exists non negative integers a b c dso that n = a² + b² + c² + d². As examples
79 = 7² + 5² + 2² + 1²
301 = 16² + 6² + 3² and 301 = 17² + 2² + 2² + 2²
2019 = 44² + 9² + 1² + 1²
2020 = 44² + 8² + 4² + 2²
The problem is to design software to find an algorithm forrepresenting a particular positive integer n as the sum of
at most 4 positive integers. For a particular positive integer n,the algorithm must find at most 4 positive integers a,
b, c, d with n = a² + b² + c² + d².
If n is a perfect square, then n equals its square root squared andI’m done.
After some thought, I decide I need a function called the integersquare root. The integer square root of a positive
integer n abbreviated isqrt( n ) is the largest positive integerwhose square is less than or equal to n. It equals the
integer part of the square root of n. For example, isqrt( 12 ) is 3since sqrt( 12 ) = 3.464 and isqrt( 301 ) = 17 since
sqrt( 301 ) is 17.349.
If n is not a perfect square, then n may be the sum of 2 squares.That is, n is the sum a² + b² for some positive
integer a between 1 and the integer square root of n. Here I willuse a greedy approach. Set a to isqrt( n ) and
b = n – a². If b is a perfect square, then I’m done. If notdecrement a by one, reset b and test if b is a perfectsquare.
Continue while b is not a perfect square and a is greater than0.
For n = 12, a = isqrt( 12 ) = 3 and b = 12 – 3² = 3, not a square.Decrement a to 2. Reset b to 12 – 2² = 8, not a
square. Decrement a to 1. Reset b to 12 – 1² = 11, not a square.Decrement a to 0. So 12 is not a sum of 2 squares.
For n = 301 I will test whether n is a sum of 2 square with atable.
n a = isqrt( n ) b = n – a²
301 17 301 – 17² = 12
16 301 – 16² = 45
15 301 – 15² = 76
14 301 – 14² = 105
13 301 – 13² = 132
12 301 – 12² = 157

1 301 – 1² = 300
An exhaustive search indicates 301 is not a sum of 2 squares.
If n is not a perfect square and not the sum of 2 squares, then nmay be the sum of 3 squares. That is, set
a to isqrt( n ) and then test whether b = n – a² is a sum of twosquares using the algorithm in the previous case. If b
is not the sum of 2 squares, decrement a, reset b and test if b isthe sum of 2 squares. Continue while b is not the
sum of 2 squares and a is greater than 0.
So I need a function for the case of testing whether n is the sumof 2 squares. I will call the function sum2Squares().
Its parameters are n and an array of size 2 with initial values ofzeros. Its return type is void. If n is the sum of 2
squares. I’ll put the squares in the array parameter. If n is notthe sum of 2 squares, the the array stays the same
with 2 zeros. The pseudo code for algorithm I used in theconstruction of the table for n = 301 follows.
Initialize a to isqrt( n ) and b to n – a².
while a > 0 and b is not a square
Decrement a.
Reset b to n – a²
if a > 0
Set the array parameter to a and isqrt( b )
The header for this function is
void find2Squares( int n, int [ ] values )
I will use another function for testing whether n is the sum of 3squares. It will use the function find2Squares. Its
algorithm is similar to the one for 2 squares but instead oftesting is b is a square, it tests whether b is the sum of 2
squares. That is,
Initialize a to isqrt( n ) and b to n – a².
Call find2Squares( b, values )
while a > 0 and values[ 0 ] == 0
Decrement a.
Reset b to n – a²
Call find2Squares( b, values )
if a > 0
Set the array parameter to a, values[ 0 ] and values[ 1 ]
The header for this function is
void find3Squares( int n, int [ ] values )
If n is not the sum of 3 squares, I repeat a similar argument forthe last time with a function find4Squares().
Initialize a to isqrt( n ) and b to n – a².
Call find3Squares( b, values )
while a > 0 and values[ 0 ] == 0
Decrement a.
Reset b to n – a²
Call find3Squares( b, values )
if a > 0
Set the array parameter to a, values[ 0 ], values[ 1 ] and values[2 ]
The header for this function is
void find4Squares( int n, int [ ] values )
I will simulate find4Squares() for n = 301 and values = { 0, 0, 0,0 } using a table.
n a b
301 17 301 – 17² = 12 find3Squares( 12, values )
12 has isqrt( 12 ) of 3 with b = 12 – 3² = 3
find2Squares( 3, values ) fails.
16 301 – 16² = 45 find3Squares( 45, values )
45 has isqrt( 45 ) of 6 with b = 45 – 6² = 9
find2Squares( 9, values ) returns with values of { 3, 0 }
Then find3Squares( 45, values ) returns with values of { isqrt( 45) = 6, 3, 0 }
So find4Squares( 301, values ) returns with values of { 16, 6, 3, 0}
My functions appear to work. Try n equal to a small 4 digit number,1015.
n a b
1015 31 1015 – 31² = 54 find3Squares( 54, values )
54 has isqrt( 54 ) of 7 with b = 54 – 7² = 5
find2Squares( 5, values ) returns with values of { 2, 1 }.
Then find3Squares( 54, values ) returns with values of { 7, 2, 1}
So find4Squares( 1015, values ) returns with values of { 31, 7, 2,1 }
One more simulation for n = 1017 follows.
n a b
1017 31 1017 – 31² = 56 find3Squares( 56, values )
56 has isqrt( 56 ) of 7 with b = 56 – 7² = 7
find2Squares( 7, values ) fails.
Reset a to 6, b to 56 – 6² = 20
find2Squares( 20, values ) returns with values of { 4, 2 }.
find3Squares( 56, values ) returns with values of { 6, 4, 2 }
find4Squares( 1017, values ) returns with values of { 31, 6, 4, 2}
The translation of the pseudo code to C code for each of thesefunctions is straightforward.
/* Return the integer square root of a positive integer n, thelargest integer whose square ≤ n.
*/
int isqrt( n ) {
return ( int )sqrt( n );
}
To use the sqrt() function requires an include for the mathlibrary. The following statement must appear near the
start of the program.
#include <math.h>
C does not have a boolean type, but it has a bool library withvalues of true and false. To test whether a positive
integer is a perfect square uses the following function.
/* Decide if n is a perfect square.
*/
bool isSquare( int n ) {
int rootN = isqrt( n );
return ( n == rootN * rootN );
}
I need the statement #include <bool.h> near the start of theprogram.
The translation to C for the function find2Squares() looks likeJava code.
/* Find, if possible, at most 2 non negative integers in thearray
squares for which the sum of the squares of its entries equalsn.
If not possible, squares[ 0 ] will be zero.
*/
void find2Squares( int n, int squares [ ] ) {
int a = isqrt( n );
if ( isSquare( n ) )
squares[ 0 ] = a;
else {
int b = n – a * a;
while ( ( a > 0 ) && ( ! isSquare( b ) ) ) {
a–;
b = n – a * a;
}
if ( isSquare( b ) ) {
squares[ 0 ] = isqrt( b );
squares[ 1 ] = a;
}
}
}
A similar translation applies for find3Squares() andfind4Squares().
/* Find, if possible, at most 3 non negative integers in thearray
squares for which the sum of the squares of its entries equalsn.
If not possible squares[ 0 ] will be zero.
*/
void find3Squares( int n, int squares [ ] ) {
find2Squares( n, squares );
if ( squares[ 0 ] == 0 ) {
int a = isqrt( n ),
b = n – a * a;
while ( ( a > 0 ) && ( squares[ 0 ] == 0 ) ) {
find2Squares( b, squares );
if ( squares[ 0 ] > 0 )
squares[ 2 ] = a;
else {
a–;
b = n – a * a;
}
}
}
}
/* Find at most 4 non negative integers in the array squares forwhich
the sum of the squares of its entries equals n.
*/
void find4Squares( int n, int squares [ ] ) {
find3Squares( n, squares );
if ( squares[ 0 ] == 0 ) {
int a = isqrt( n ),
b = n – a * a;
while ( ( a > 0 ) && ( squares[ 0 ] == 0 ) ) {
find3Squares( b, squares );
if ( squares[ 0 ] > 0 )
squares[ 3 ] = a;
else {
a–;
b = n – a * a;
}
}
}
}
I decide to test find4Squares() with a for loop in the main()function after asking the client for low and high values of
the for loop. That is,
Declare int variables low and high.
Print the message “Enter low and high values for four squaresproblem.”
Use scanf() function to get low and high.
for ( int i = low; i <= high; i++ )
Declare an integer array called values initialized to fourzeros.
Call find4Squares( i, values )
Print i and values with a void function printSquares() andparameters I and values.
C uses the function scanf() to obtain the value of a variable fromthe keyboard. The statement scanf( “%d”, &low )
assigns the integer the client enters to the int variable low.Notice the use of the address operator & before the
variable low. To get low and high from the keyboard requires scanf(“%d %d”, &low, &high )
The function printSquares() prints i and the nonzero items in thearray values. I must use a while loop to print the
non zero items in values.
/* Print n and the non zero items of the values array.
*/
void printSquares( int n, int values [ ] )
Print n.
Declare an integer i intialized to 0.
while values[ i ] > 0
Print values[ i ]²
Increment i.
Print the new line character.
I’ll provide you with the actual C code in a separate file and withthe commands needed to compile and run the
code.
The purpose of this note was to illustrate what I expect from youwhen I ask you to write a design for an assignment
and to introduce you to the C language.
The syntax of C is very similar to the syntax of Java. In fact thedesigner for the Java language decided to use its
syntax to entice C programmers to switch to Java.
One key difference is that C has no class (but C++ does).
Another key difference is that C has something called struct (forstructure) and pointers. I’ll treat structs and pointers
later in the course.
Now here’s your first assignment

Design Assignment 0 : Design software to verify a statementabout positive integers.
The overall objective of your first design assignment is to improveyour ability to solve problems. In particular, you
will design functions that use either a for loop or a while loop toverify the following statement about positive
integers:
A positive integer greater than 2 can be written as a sum of twopositive integers k and m such that 2k
+ m is prime.
As examples, 2 = 1 + 1, 3 = 2 + 1, 4 = 1 + 3, 20 = 5 + 15, 50 = 11+ 39, 112 = 1 + 111, 666 = 7 + 659,
1000000 = 7 + 999993
You must design an algorithm to express a positive integer greaterthan 1 as a sum k + m where 2k
+ m is a prime
number. A brute force algorithm that uses a while loop isacceptable.
A design uses paragraphs that explain the steps you need to solvethe problem. I will read pseudo code only if
explanation in English precedes it.
Once you have an algorithm you must provide a simulation with atable for your algorithm with one or two
representative small positive integers.
You must also design another function to count the number ofdifferent ways to express a particular positive integer
as a sum k + m where 2k
+ m is prime.
As examples, the count for 10 is 3 since 10 = 1 + 9 = 5 + 5 = 7 +3, the count for 20 is 2 since 20 = 5 + 15 = 9 + 11
and the count for 112 is 8 since
112 = 1 + 111 = 5 + 107 = 7 + 105 = 13 + 99 = 23 + 89 = 25 + 87 =41 + 71 = 47 + 65
For the function that performs the count, I must warn you that thecomputation of 2
k
+ m will overflow for k > 30 for
integer type and for k > 62 for long type.
Your design must indicate the calls to each of these functions. Itmust also indicate what you will do to test each
function somewhat rigorously.

Expert Answer


Answer to Problem. Write positive integer n as a sum of at most 4 squares. Mathematicians can prove that a positive integer can be…

OR