# Last X elements request

Hi,

I have to connect to a server with basic function. One function give me the number of elements and one function give me the elements after I provide the page which I want and the size of the page which can be biiger that 100

What I want is to get last X elements from the list with one call.

Example: I have 230 elements from one request to get last 29 elements. One first look solution is page 5 with 50 elements on the page and remove the top until remain 29 elements

My question is: How can calculate in the proper way wich is the last page and size of the page that give to me my elements?
###### Who is Participating?

Commented:
As per you explanation, I think you need to get last X elements which can not be more then 100.

Suppose you need last X elements, which is less then 100.
you have N elements in the table or database.

your question is how to calculate the size of the page:
you can use the below algorithm:
- for i = X to i<=100 {
if(N%i >= X){
then i is the page size
}
}

now page size = i
Page Number = Floor(N/i) + 1

Here % is the modules operation.
Ex: 8%3 = 2 and 17%4 = 1
0

Commented:
I suppose you would start by getting the last page. Check the number of elements. If it is too much, remove some. If it is not enough, get the next to last page and repeat.
0

Author Commented:
I have to do ONE request.

I know the number of elements and my page size have 100 elements limit.
0

Commented:
I don't fully understand the question yet.

"size of the page which can be biiger that 100"
"my page size have 100 elements limit."

So how big can the pages be?

Are you just trying to grab the last X elements from a page? And you know the number of elements on the page? So if there are N elements on the page and you want the last X, you just grab from N-X+1 to N?
0

Author Commented:
No

I can change the size of the page, but not more that 100.
I can request which page I want and also I can set up the size of the page.

Idea is some matematic formula or something close to .. that tell me. If you want to get last 23 elemets from 111 you have to request the page with number 2 (which I have to calculate) and the size of the page (for all the page from 1 to the last wich is 2 in this case) is 56 elements per page. And you will get 55 elements and you get from that the last 23.

I have to think which is the last page and which is the proper size for a proper request.

Thanks
Petre
0

Commented:
So there's 56 elements in the first page and 55 in the second and you want the last 23, then N is 55 and X is 23 so you want the pages from N-X+1 to 55 which is 55-23+1 to 55 or 33 to 55?
0

Author Commented:
no ... this is a simple case ... we have something like this:

case1: get last 44 elements from 745 elements
case2: get last 21 elements from 891 elements
case3: get last 13 elements from 311 elements

all request from one request. So I know the numer of elements and the number of last elementes that I want to get.

there are one solution:  first page and the size of page all elements - not a good solution

but this solution generate a lot of trafic if we get all elements in one page (time out  request, huge trafic .....)

so I have to find a better way to get last elements when I know the number of elements from one request

i can do one request with two parameters:
- which page I want
- which is the general page size

Practical I have to calculate which is the proper page size and the number of pages.

thanks
Petre
0

Commented:
I still don't really get it. Pages of what? Is this regarding the Internet, or RAM, or what? Is this a programming question? What language?
0

Author Commented:
Hi,

Any language, I want to find a way to doit. How to calculate the page of size and last page.

Think like this. You have a collection of objects which you have to split in lot of objects (when the lots have the same number of elements), there are situation when last lot is smaller that rest of lots. I need to splet in some way this collection in a way that last lot have more that an number of elements (which i know) and to see how many elements is in a general lot and which is the last lot.

In the real life I have a service which have a function which allow to me to send the page which I want and the size of the page. I have from one request to get the last X elements using that function. What is help me is the fact that I know the total number of elements.

How can I calculate the size of the page and which is the las page? to get the last X elements with best performace. (You can call page 1 with the total number of elements but this is not a proper request)

Thanks
Petre
0

Commented:
Hope this algo will be helpful. If not please provide what you are missing?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.