最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Most efficient javascript algorithm to calc total if amount were to double each day - Stack Overflow

programmeradmin0浏览0评论

Give N days, with money amount doubling each day, is this the most efficient way to acplish this?

Day one: you are given $.5.
Day two: you are given twice the amount as day one $1, now you have $1.5
Day three: you are given twice the amount as day two $2 and now you have $3.5
And so on.

function calcit3()
{
  var cur_total = .5;
  var prev_total = 0;
  var days = 20;

  for ( z = 1; z < days; z++ )
  {
    cur_total = cur_total * 2;
    prev_total = cur_total;
  } 

  return (cur_total + prev_total);
}

This is just purely acedemic. Not really trying to shave cycles or anything.

Thanks.

EDIT:

Give N days, with money amount doubling each day, is this the most efficient way to acplish this?

Day one: you are given $.5.
Day two: you are given twice the amount as day one $1, now you have $1.5
Day three: you are given twice the amount as day two $2 and now you have $3.5
And so on.

function calcit3()
{
  var cur_total = .5;
  var prev_total = 0;
  var days = 20;

  for ( z = 1; z < days; z++ )
  {
    cur_total = cur_total * 2;
    prev_total = cur_total;
  } 

  return (cur_total + prev_total);
}

This is just purely acedemic. Not really trying to shave cycles or anything.

Thanks.

EDIT:

Share Improve this question edited Dec 5, 2010 at 21:21 cHao 86.6k20 gold badges146 silver badges177 bronze badges asked Dec 5, 2010 at 20:13 evetsevets 5791 gold badge5 silver badges12 bronze badges 8
  • 1 Is this homework? Not that there's anything wrong with it, but you should tag it as such if it is. – Frédéric Hamidi Commented Dec 5, 2010 at 20:17
  • Na, not homework. Just a watercooler question and everyone had a different answer. – evets Commented Dec 5, 2010 at 20:21
  • Heh, homework spotters are always on vigil :) Obviously it is not, because its is well known problem from school algebra course en.wikipedia/wiki/Geometric_progression – Free Consulting Commented Dec 5, 2010 at 20:38
  • Your estimation of the result is incorrect. I suspect you're basing it on the code you've posted, which is wrong - try out your code with days = 3 to see an example. – Anon. Commented Dec 5, 2010 at 20:43
  • Check your math. If i start with $.50, and i'm given $1 on day 1, $2 on day 2, $4 on day 3, etc...that is, if every day i'm adding twice as much...there's no other $.50 to make this a whole number. Something's screwy about your numbers. – cHao Commented Dec 5, 2010 at 20:45
 |  Show 3 more ments

4 Answers 4

Reset to default 9

The code you've provided doesn't do what the description says it should.

If the initial amount is a then the amount you get on the i th day is a * 2 ^ i, and the sum after n days is the sum of that from 0 to n.

Simplifying, we get:

a * (2 ^ (n+1) - 1)

No looping necessary.

If I understand the problem correctly, it's just a simple geometric progression starting with 0.5 and doubling in value each day. The total is nothing but the sum to n terms of this series, which is:

a * (r^n - 1)
-------------
   r - 1

Here a = 0.5, r = 2; substituting yields the formula:

0.5 * (2^n - 1)

Or equivalently in JavaScript:

return 0.5 * (Math.pow(2, days) - 1);

Is this correct?

return parseInt(Array(days).join('1'), 2) + 0.5

How about:

return (Math.pow(2.0, days + 1) - 1) * initial_amount;

No iteration necessary. Given an initial_amount of .5, after 1 day, you'll have (2^2-1) * .5 == 1.5, after 2 days you'll have (2^3-1)*.5 == 3.5, etc.

Note, this assumes initial_amount on day 0, not 1. If you're intent on having day 1 be the start, just remove the + 1 from the expression.

Also note, if you're talking about money, the formula is kinda odd. Money would usually double daily, including the initial amount. So on day 1, you'd have $1, on day 2 $2, day 3 $4, etc.

发布评论

评论列表(0)

  1. 暂无评论