square chalence

General FreeBASIC programming questions.
Post Reply
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

square chalence

Post by bluatigro »

puzzle from 1980 book :

1 :
write code that calculates the number of squares in any rectangle
the recangle is filled totaly
no squares overlap
input is width and height of the rectangle

2 :
find the smalest rectangle whit that
thebigh
Posts: 43
Joined: Dec 14, 2018 11:11

Re: square chalence

Post by thebigh »

I don't know if I understand the question.

Surely if w is a rational multiple of h we can cover the rectangle with many different square tesselations. And if not, no covering is possible without leaving gaps.

Suppose mw = nh where m and n are both positive integers. Then we can cover the rectangle with squares of side length s = w/n = h/m.

But we can also do it with squares of side length s/2, s/3, s/4 and so on.


The second part of your question is incomprehensible.
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: square chalence

Post by bluatigro »

sizes are integers

find the smalles retangle that fits the puzle
thebigh
Posts: 43
Joined: Dec 14, 2018 11:11

Re: square chalence

Post by thebigh »

1. Smallest number of squares that will completely and nonoverlappingly cover a rectangle of given integer width and height

Code: Select all

function gcd( w as uinteger, h as uinteger ) as uinteger
    if h = 0 then
        return w
    else
        return gcd(h, w mod h)
    end if
end function

dim as uinteger w, h, g
input w, h
g = gcd(w, h)
print w*h/(g*g)
edit: smallest number of squares of equal size

2. w=h=1 is the smallest rectangle with integer side lengths that can be totally covered by some number of squares. Again, I don't think this question makes sense.
Last edited by thebigh on Apr 02, 2020 16:13, edited 1 time in total.
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: square chalence

Post by bluatigro »

more that 1 square is good
Post Reply