darbid
darbid

Reputation: 2721

How to calculate some points given a range of numbers

Math was a long time ago and I am not sure what I want, nor how to calculate it. Imagine I know I have the numbers from 1 to 100. I want to give the user a picker/combination box to be able to select some of these numbers. But I do not want all the numbers in the picker. Maybe it shows the numbers 1,2,3,4,5,10,15,25,40,60,80,90,100. In my words it goes up slowly in the beginning and then becomes steeper later. I think that an exponential function is too steep.

Ultimately I would love it if someone could show me how to create an objective C method which accepts two numbers and returns and array of NSNumbers similar to what I have shown above.

Thanks in advance everyone.

Upvotes: 1

Views: 347

Answers (3)

DPenner1
DPenner1

Reputation: 10452

I'm not 100% sure what you're looking for, but you could use a simple equation such as:

f(x) = floor(xc) where c > 1

You could set c to whatever number gives the desired steepness. Eg. For c = 1.5, you would have:

0, 1, 2, 5, 8, 11, 14, 18, 22, 27, 31, 36, 41, 46, 52, 58, 64, 70, 76, 82, 89, 96, 103, 110, 117, 125...

To adjust for a different starting value, s, just do f(x, s) = floor(xc) + s.


Another possible function would be one that consists of a series of lines slowly increasing in steepness. For example, here is a "parabola" made of line segments:

f(x, s) = 0.5cd(floor(x/d))2 + 0.5cd(floor(x/d)) + c(floor(x/d) + 1)(x mod d) + s

where c and d are constants (≥ 1) of your choosing (and the derivation is slightly irritating). c is the slope of the initial line, d represents how long the line segments are. s is again the starting value. For example, let s = 100, c = 3, d = 4, and you have the following sequence (for x = 0, 1, 2, ...):

100, 103, 106, 109, 112, 118, 124, 130, 136, 145, 154, 163, 172, 184, 196, 208, 220, 232, 247...

Now if we wanted "round" numbers we would set c and s to a "round" number. But if we wanted to start counting by ones, we could define something like:

g(x) = x             when x = 0, 1, 2, 3, 4
     = f(x - 5, 5)   when x ≥ 5

So if c = 5, and d = 5, we have a sequence like:

0, 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 95, 110, 125, 140, 155, 175, 195, 215, 235, 255, 280, 305, 330, 355, 380, 410, 440, 470, 500, ...


*And of course, to stop before the given max value, just stop evaluating the function when it returns a number greater than the desired max.

Upvotes: 0

meth
meth

Reputation: 1885

i have written some code to generate

-(NSArray *)generateNumbersFromBeginning:(int)begin toEnd:(int)end withCount:(int)count
{
    NSMutableArray * genNumbers=[NSMutableArray new];

    double sqBegin=1.0; //our beginning generator

    double sqEnd  =sqrt((double)(end-begin)); // last element to produce

    double step = (sqEnd-sqBegin)/((double)count);//

    double power=1.0;

    NSLog(@"%f %f %f",sqBegin,sqEnd,step);

    for (int a= 0; a<count; a++) {

        sqBegin+=step;

        power += 1.0/(double)count;

        int genNumber=(int)(pow(sqBegin, power));

        genNumber+=begin;

        [genNumbers addObject:[NSNumber numberWithInt:genNumber]];

    }

    NSLog(@"numbers are: %@ ",genNumbers);

    return genNumbers;


}

the usage is ([a,b], x) where a is start point b is end point and x is the number of int generated in that range.

e.g:

[self generateNumbersFromBeginning:9000 toEnd:10000 withCount:36];

the result of this call is:

numbers are: (
    9001,
    9002,
    9003,
    9005,
    9006,
    9008,
    9010,
    9012,
    9014,
    9017,
    9021,
    9025,
    9029,
    9034,
    9041,
    9048,
    9056,
    9065,
    9076,
    9089,
    9104,
    9121,
    9141,
    9165,
    9192,
    9223,
    9259,
    9301,
    9350,
    9407,
    9473,
    9549,
    9638,
    9741,
    9860,
    9999
) 

Upvotes: 0

Thanakron Tandavas
Thanakron Tandavas

Reputation: 5683

How about Sequence of Triangular Number ?

Equation : xn = n(n+1)/2

Sequence (start at n = 0) : 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, ...

Input : 1 to 100

Min value = 1 , Max value = 100

1 + 0 = 1

1 + 1 = 2

2 + 3 = 5

5 + 6 = 11

11 + 10 = 21

21 + 15 = 36

36 + 21 = 57

and so on.. until the evaluated number exceeds max value

Sequence : 1, 2, 5, 11, 21, 36, 57 and so on ...

Input : 1000 to 10000

Min value = 1000 , Max value = 10000

1000 + 0 = 1000

1000 + 1 = 1001

1001 + 3 = 1004

1004 + 6 = 1010

1010 + 10 = 1020

1020 + 15 = 1035

1035 + 21 = 1056

and so on.. until the evaluated number exceeds max value

Sequence : 1000, 1001, 1004, 1010, 1020, 1035, 1056 and so on ...

I will leave the implementation part for you to try it out.

Upvotes: 1

Related Questions