Select X# of items from a total list at the furthest possible intervals.

dgolberg

New member
Joined
Sep 7, 2012
Messages
10
Ok, so this one's got me baffled to the point that, I'm not even sure what category this falls in (so if this post is in the wrong one, I apologize for that), but I'm assuming one of the more advanced forms of math. ;) Unfortunately, I only really remember up to basic, possibly intermediate algebra due to lack of use.

Anyway, what I'm trying to do is find a set number of items out of a total number of items while keeping the intervals as far apart as possible. This is part of a script I'm attempting to write for work, but it's proving more difficult than originally expected as 2 of the variables change quite a bit and my math skills have dropped off a bit, so it's not so much the result as the equation to get the result that I'm having trouble with.

To start, let's say I have a total of Z items of which I need to get X of those items at regular intervals. Now, here's the kicker, the intervals must be in whole numbers. So, for example, say Z = 12 and X = 4; (additionally, 0 will be the first item to grab; think of it more as a stall number than a number of items) I would need the equation to return 0, 4, 8, and 12 as the items to grab. Now, the hard part is when Z and/or X change. Let's say Z = 93 and X = 36; how would I figure which items are the ones I need to grab (the first item being from stall 0 and the 36th from stall 93, the rest being as evenly spaced as possible in between)? Obviously, it's not always going to be a set number of skips before I grab something (like the z=12 x=4 is grab 1, skip 3, grab 1, skip 3, etc). Anyone have any ideas how to write this equation? :confused: I've been staring at this problem so long, it's probably about to slap me across the face, but my mind's so numb towards the issue that I can't see it.

Anyone have any ideas?

Thanks in advance for any assistance!

Edit: Whoopsie... had a typo in there "grab 1 skip 2" instead of "grab 1 skip 3"... sorry, I'm tired and my brain's fried, lol.
 
Last edited:
Ok, so this one's got me baffled to the point that, I'm not even sure what category this falls in (so if this post is in the wrong one, I apologize for that), but I'm assuming one of the more advanced forms of math. ;) Unfortunately, I only really remember up to basic, possibly intermediate algebra due to lack of use.

Anyway, what I'm trying to do is find a set number of items out of a total number of items while keeping the intervals as far apart as possible. This is part of a script I'm attempting to write for work, but it's proving more difficult than originally expected as 2 of the variables change quite a bit and my math skills have dropped off a bit, so it's not so much the result as the equation to get the result that I'm having trouble with.

To start, let's say I have a total of Z items of which I need to get X of those items at regular intervals. Now, here's the kicker, the intervals must be in whole numbers. So, for example, say Z = 12 and X = 4; (additionally, 0 will be the first item to grab; think of it more as a stall number than a number of items) I would need the equation to return 0, 4, 8, and 12 as the items to grab. Now, the hard part is when Z and/or X change. Let's say Z = 93 and X = 36; how would I figure which items are the ones I need to grab (the first item being from stall 0 and the 36th from stall 93, the rest being as evenly spaced as possible in between)? Obviously, it's not always going to be a set number of skips before I grab something (like the z=12 x=4 is grab 1, skip 3, grab 1, skip 3, etc). Anyone have any ideas how to write this equation? :confused: I've been staring at this problem so long, it's probably about to slap me across the face, but my mind's so numb towards the issue that I can't see it.

Anyone have any ideas?

Thanks in advance for any assistance!

Edit: Whoopsie... had a typo in there "grab 1 skip 2" instead of "grab 1 skip 3"... sorry, I'm tired and my brain's fried, lol.

By script,

do you mean that you are writing a computer program

or

are you trying to use a spreadsheet program like ms-excel?
 
I'm writing a computer program... I just use excel to help me keep track of long equations when I don't have a piece of paper handy. The script part of it is irrelevant (converting an equation to script is easy if you know the equation), I just can't quite figure out the equation to find the intervals at which I'm supposed to get the items.
 
To distribute 36 items in 94 (0 to 93) locations, note that 36 divides into 94 about 2.6 times. That lies between 2 and 3 so if we were to alternate steps of 2 and 3, that is putting 2 objects every 5 steps, we could come pretty close: objects at 0, 2, 5, 7, 10, 12, 15, 17, 20, 22, 25, 27, 30, 32, 35, 37, 40, 42, 45, 47, 50, 52, 55, 57, 60, 62, 65, 67, 70, 72, 75, 77, 80, 82, 85, 87, 90, 92, ending with 94. However, because 2.6 is NOT 2.5, that gives slightly too many (39 rather than 36). We could, rather, argue that .6 = 3/5 and so use 2 steps 2/5 of the time and 3 steps 3/5 of the time. That is, of the first 35 objects to be placed (35)(2/5)= 14 be placed after steps of 2 and (35(3/5)= 21 placed after steps of 3. The exact placing is not really important- the largets step will be 3, the smallest two. One placement might be 0, 3, 6, 9, 11, 13, 16, 19, 22, 24, 26, 29, 32, 35, 37, 39, 42, 45, 48, 51, 53, 55, 58, 61, 65, 67, 69, 72, 75, 78, 80, 82, 85, 88, 91, 94. Exactly 36 objects between 0 and 94.

The basic idea behind all that to plane m object from 0 to N, divide N+1 by m. If that is an integer it's easy- use that integer as the step. If it is not, use the nearest "easy" fraction. To divide between the next higher integer and the next lower.
 
That sounds like the right track. The thing I'm having the trouble with is how to determine when to do the 2 steps and when to do the 3 (as well as how to adjust this automatically should the number(s) change). It sounds like I was on the right track with what you're saying, but I did end up with the 39 instead of 36 like you said would happen. For example, here was my last attempt:

x = 93
y = 36
D = y / x; //in this case, 0.387097

z = 1

for(i = 0;i < x;i++) //script loop to cycle through all the numbers starting at 0
if z greater than or equal to 1, grab the item and subtract 1 from z
if z is NOT greater than or equal to 1, add D to it and skip this item

This was the closest equation I had that I can recall. Any ideas? Does it make sense (I'm not sure if I described that right since part of it is in my head in script)?

Edit: I got ninja-ed! lol, I'll take a look at the other post now...
 
If I understand your problem, there is more than one way to solve it. Here is a simple one.

To start, you will need the floor function, which I shall show as F(a).

Number of stalls = z + 1.

Stalls numbered consecutively from 0 through z.

Number to be selected = x, but stalls 0 and z are always to be selected. Distance between selections is to as constant as possible.

IS THAT THE PROBLEM?

\(\displaystyle Let\ s_n\ be\ selection\ n \implies s_1 = 0\ and\ s_x = z.\)

\(\displaystyle 1 < n < x \implies S_n = S_{n-1} + F\left(\dfrac{z - s_{n-1}}{x + 1 - n}\right).\)

I think that should do it.

That's pretty close, I'm just not quite sure how to translate that equation to fit the loop. Take a look a my post you ninja-ed :p Perhaps that should make it easier to describe for a math dumby like me, lol.

Edit: Ok, honestly, I'm not even quite sure how to read that equation now... :( College algebra... you've failed me yet again! Why must you slip away so easily?! lol
 
Last edited:
EDIT: The above is easy to in Excel, where I think the floor function is =INT, but I do not currently have access to Excel so I may be wrong. In a program of course, you'd write a loop, and getting the floor function depends on what language you are using. I should also say that I have not tested or proved that formula; I am sitting on the beach with my family and dog and may have goofed up when looking at girls. So test it carefully.

LOL! Completely understandable, and I thank you for taking the time out of your vacation (?) to help me with this. The script I'm working with is a form of javascript, by the way (modified for Photoshop). Edit: Math.floor() is the javascript function to round down. Excel is simply =Floor()
 
Last edited:
I have not written code in over forty years. I'll try to get my son who is a software engineer to translate my math formula into code, but we leave for home tomorrow so he is getting beach time in now. Since the formula is recursive, it should fit nicely into a loop.

Awesome! Thank you very much for the assistance with this! I'm not much of a script writer yet myself. I went for a more generalized degree and swore I'd never be a programmer when I graduated... though now that's primarily what I've been doing as it fits nicely with my actual job of process development (except in instances such as this where I know something's possible, but not how to go about it yet, lol). Again, thank you very much for the help with this!
 
OK. My son wrote a little routine in Python to implement my formula. Here is the URL:

http://pastebin.com/1CfUa46R

He asked me to point out that things have been adjusted to reflect that I was assuming an initial value of 1 for the loop, which is considered insane in Python. He says you may need to be sensitive to how loops in JavaScript default.

He also told me to advise you not to ask me questions about coding because even Babbage was more sophisticated as a coder. Stackoverflow.com might be a good resource for coding questions.

Actually, you and Denis both have helped a lot. I also just realized that my script blocker was blocking your post with the math equation so I was seeing it as is written below rather than as it was intended (hence the confusion, lol):
Code:
[tex]Let\ s_n\ be\ selection\ n \implies s_1 = 0\ and\ s_x = z.[/tex]

[tex]1 < n < x \implies S_n = S_{n-1} + F\left(\dfrac{z - s_{n-1}}{x + 1 - n}\right).[/tex]

This had me scratching my head asking what I had gotten myself into, lol. A LOT easier to read when in the proper format, lol! :p

And yes, starting at 1 does confuse javascript as things such as arrays start at a value of 0 rather than 1 (the data I'm grabbing is, in fact, being stored in an array), hence the 0-12 rather than 1-13

Props to your son on the comparison of you to Babbage, btw! :p Anyway, I'll give yours and Denis's suggestions a try and see how it goes. Thanks again for the help guys!
 
Last edited:
Hmm, I'm having a little trouble translating a couple parts of your son's code to javascript. I'm not too familiar with Python, so there are a couple things I'm unsure about.


  1. s = [0] * x

  2. s_prev = s[n - 1]

In javascript (or at least the version I'm working with), the [] brackets are typically only used when dealing with an array of some sort, so I'm not sure how Python handles these. Could your son elaborate a little on what the 2 lines above would mean or equate to? Is the s = [0] * x part an alternate way to create arrays in Python while s_prev = s[n - 1] is referencing a specific point in the array? Just want to make sure I'm on the right track here if this is the case. :confused:
 
Thanks for your help again Jeff, and I do apologize for bothering you further with this, but I wanted to be sure that I was interpreting that correctly and that it was not, in fact, part of the equation you had originally given me. I've tried asking for help with this issue in an actual scripting forum for Photoshop, but apparently was not able to describe the issue with enough clarity for them to assist me with the problem I was having. I did get one solution, but it involved altering the loop to match the equation rather than the equation to match the loop. While that method would work, it would not be nearly as efficient as the method using your equation.

Again, I thank you for the assistance and apologize for straying off course of the subject matter of this forum.
 
Top