Algorithm for viewing a larger container on a small screen

I need a mathematical algorithm (or not) simple (or not).

It looks like this: I have two numbers a and b, and you need to find the smaller number closer to b, c. Such that "a% c == 0"

If "a% b == 0", then c == b

Why? My screen has a size of x pixels. And the container has y pixels such that y> x.

I want to calculate how much I have to scroll so that I can see my container on my screen without losing space. I want you to be sure to browse.

I need to know how much I need to roll, according to my screen and how often to view the entire container.

+4
source share
2 answers

Will this help you? (Java code)

int a = 2000; int b = 300; int c = 0; for (int i = b; i > 0; i--) { if ( (a % i) == 0) { c = i; break; } } 

The result will be in c.

+2
source

The task defines, given a and b , find the largest c such that

  • c <= b
  • c*k = a for some k

The first constraint places the lower bound on k , and maximizing c equivalent to minimizing k , taking into account the second constraint.

The lower bound for k is given by

 a = c*k <= b*k 

and therefore k >= a/b . Therefore, we just look for the smallest k , which is a divisor of a , for example.

 if (b > a) return a; for (int k=a/b; k<=a; ++k) if (a % k == 0) { return a/k; } } 
0
source

All Articles