loops - Convert linear division function into logarithimic -


im not sure if using correct mathematical terminology here goes...

i have function divides number equally number.

function factorit(numpieces, totalsize) {     return totalsize / numpieces; } 

this returns linear/even division. need return values form uneven/exponential/logarithmic set.

for example, running function above 10 times, on values 500 , 10 return 50 ten times, because 500 divided 10 50. need return more like:

1, 2, 5, 10, 20, 30, 60, 100, 150, 200 // should add input value 

these 10 values add 578, function needs add input value of 500. far ive got..

var output = ''; (var = 1; i<11; i++) {     output += factorit(i, 10, 500) + '<br>'; } function factorit(i, numpieces, totalsize) {     var factor = 5;     return totalsize / numpieces / * factor; // err...something this... :-s } 

the ultimate objective of function plot bitmaps on 2 dimensional space ever-decreasing space between them, in order create illusion of 3d receding perspective.

does know of formula need this? can't seem wrap head around it...

your question doesn't specify how sequence "1, 2, 5, 10, 20, 30, 60, ...". no specify if need values integers.

if want "quasi-exponential" sequence adds given total, use succesive division fixed factor like:

var current = 500; while (current > 0) {   var new = ceil(current / 2);   output = new + '<br>' + output;   current = current - new; } 

this generate "1, 2, 4, 8, 16, 31, 63, 125, 250" sum 500. ceil ceiling function, i.e. biggest integer greater or equal argument (always "rounding up"). use different factor produce shorter sequences (smaller factor) or longer ones.

note loop produces values in revesed order, i.e. starting biggest value. approach might produce duplicate values of 1 @ beginning of sequence (if starting power of 2...)

also, approach doesn't let specify number of elements in sequence. if want have fixed-size sequences, try compute factor similar to

f = exp(ln(total)/n) 

but again, if operate on integer values, factor might need modified correct result.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -