Computer Science, asked by dhaneeshaoruganti, 9 months ago

A positive integer m can be expresseed as the sum of three squares if it is of the form p + q + r where p, q, r ≥ 0, and p, q, r are all perfect squares. For instance, 2 can be written as 0+1+1 but 7 cannot be expressed as the sum of three squares. The first numbers that cannot be expressed as the sum of three squares are 7, 15, 23, 28, 31, 39, 47, 55, 60, 63, 71, … (see Legendre's three-square theorem).

Write a Python function threesquares(m) that takes an integer m as input and returns True if m can be expressed as the sum of three squares and False otherwise. (If m is not positive, your function should return False.)

Here are some examples of how your function should work.

Answers

Answered by plpaikrao
5

Answer:

function checkthrees(t1)

z=0;

t=floor(sqrt(t1));

for i=0:t

for j=0:t

for k=0:t

if ((i*i+j*j+k*k)==t1)

z=z+1;

else

z=z+0;

end

end

end

end

if (z==0) disp('False');

else disp('True');

end

end

Explanation:

I dont know Python programming now, but this is the MATLAB code for the said function.

Answered by poojan
1

Python program to " Legendre's three-square theorem".

Language used : Python Programming

Program :

import math

m=int(input())

def threesquares(m):

   if m>=0:

       x=list(range(0,math.ceil(math.sqrt(m)+1)))

       for i in x:

           for j in x:

               for k in x:

                   if (i**2)+(j**2)+(k**2)==m:

                       return True

       return False

   else:

       return False

print(threesquares(m))

Testcase 1 : (For Legendre's three-square number)

Input :

47

Output :

True

Testcase 2 : (NOT a Legendre's three-square number)

Input :

2

Output :

False

Testcase 3 : (Negative number)

Input :

-87

Output :

False

Explanation :

  • Firstly, take an integer as an input from the user and store it in variable.

  • Call the function with the input as parameter. But before that, Define the function we call, threesquares() that takes the inputted integer as a parameter.

  • On entering the function check whether it is positive or negative with >=0 condition. If negative, return False

  • If it is positive, prepare a list ranging from 0 to the ceil of square root of the parameter passed + 1. because, no element will be a perfect square to a number which is greater than the number we get by square rooting it.

  • Then run three loop one outer, 2nd inner and third innermost, all rangining from 0 to the last element of the list formed. And check whether on squaring each of three iterables current values and the resultants sum up gives the original number.

  • If yes, return True. else once all the iterations over the list completes but nothing returned, return False. That's it!

Learn more :

1) Printing all the palindromes formed by a palindrome word.

brainly.in/question/19151384

2) Indentation is must in python. Know more about it at :

brainly.in/question/17731168

3) Write a Python function sumsquare(l) that takes a nonempty list of integers and returns a list [odd,even], where odd is the sum of squares all the odd numbers in l and even is the sum of squares of all the even numbers in l.

brainly.in/question/15473120

4) Python program to find absolute difference between the odd and even numbers in the inputted number.

https://brainly.in/question/11611140

Attachments:
Similar questions