Home > OS >  Given an area in meters, repeatedly remove the largest possible square
Given an area in meters, repeatedly remove the largest possible square

Time:04-25

So I am learning python and wants to solve a problem that takes area as an integer input and calculates how many square meters can be made with it.

Example

For example, if you enter an area of 12 meters (input 12), you can make one 3x3 square meters (with a area of 9 meters). That would leave you an area of 3 meters, so you can turn them into three 1x1 square meters.

Example Input and output.

input: function(12)
output: 9,1,1,1

input: function(22)
output: 16, 4, 1, 1

input: function(15324)
output: 15129,169,25,1

I tried the following but I couldn't make it exactly.

def area(num):
    return num * num

number = float(input(" Please Enter any numeric Value : "))

area= square(number)

print(area)

I only tried to return the square number of a given number but how can I improve it based on the problem?

CodePudding user response:

I think you are close to the solution. I implemented a simple but effective solution.


import math

def square(n):
    lis = []
    
    rest = n
    while n > 0:
        rest = math.floor(math.sqrt(n))**2
        lis.append(rest)
        n-= rest
    return(lis)

You can see that, this line math.floor(math.sqrt(n))^2 is the computing of the closest perfect square lower than n. Then, I save the result in a list and I update the n value with the subtraction of n minus the rest. Iterating this procedure with a while loop, I obtain a final list with the results you specified.

square(12)
>>> [9, 1, 1, 1]
square(22)
>>> [16, 4, 1, 1]
square(15324)
>>> [15129, 169, 25, 1]

EDIT
Prompt command to execute the function.

n = float(input(" Please Enter any numeric Value : "))
square(n)

In conclusion, if you want to have an all-in function with prompt inside.


import math

def square_prompt():
    n = 1
    while n>0:
        n = float(input(" Please Enter any numeric Value : "))
        if n:
            print(square(n))

square_prompt()

NB. If you insert a number lower equals than 0, you will stop the loop (which means the user doesn't have any other number to ask to the program).

CodePudding user response:

You can do it with a while loop:

import numpy as np


def func(a):
    lst = []
    while a: # Repeat until a reaches 0
        n = int(np.sqrt(a))**2 # Largest square number < a
        a -= n # Reduce a by n
        lst.append(n) # Append n to a list and repeat
    return lst

print(func(12))
print(func(22))
print(func(15324))

Output:

[9, 1, 1, 1]
[16, 4, 1, 1]
[15129, 169, 25, 1]

CodePudding user response:

from math import sqrt

def square(n):
    lst = [] # Empty List
    while True: 
        for a in range(1,int(n)): 
            if n-a > 0:
                 if round(sqrt(n-a)) == sqrt(n-a):
                    lst.append(str((round(n-a)))) # I am check that if round of square of a number is equal to square of number because sqrt gives square for every not but I need only perfect square. Square Of 3 is 1.732 but this is a perfect square not then round(1.732) returns 2 which is not equal to 1.732.

                    n = a 
        if n==4: # This line is because 4-Anything is not a perfect square therefore I need to add a custom if statement. 
            lst.append(str(4)) # Because 4 is a perfect square of therefore I add 4 to the list.
            n = 0
            break # exit While Loop 
        if n<=4: # Because if n is 3 then I need to add 1 to the list for n times.
            break # exit While loop
    for a in range(int(n)): # add 1 to n times in lst if n is less than 4.
        lst.append('1')
    return ' '.join(lst)

def area(num):
    return num * num

number = float(input(" Please Enter any numeric Value : "))

area= square(number)

print(area)

Output:


IN:22, OUT:16 4 1 1

This is what I can explain to you.

CodePudding user response:

You can convert this problem into a find the closest square number problem, so you will need to calculate the square root of the input area and use the math library to floor this value, getting the closest square number:

import math

area = float(input(" Please Enter any numeric Value : "))
sqrt = area**0.5
closest = math.floor(sqrt)**2
print(closest, area-closest)

Output:

Please Enter any numeric Value : 12
9 3.0
  • Related