Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30517

Re: Layered object

Newsgroups comp.lang.javascript
Date 2016-05-23 08:31 -0700
References <89e1dd41-b971-4f04-aad2-e726abe20d56@googlegroups.com> <3aa6bb6e-c513-4228-ada6-095c58d67022@googlegroups.com> <0984346c-891a-4f03-96c5-c983f96aeb1d@googlegroups.com> <1426456b-46cd-4a18-8d3b-1447508dd3e6@googlegroups.com>
Message-ID <01b07c96-617e-4d2f-83bc-e85fb4079a99@googlegroups.com> (permalink)
Subject Re: Layered object
From jonas.thornvall@gmail.com

Show all headers | View raw


Den måndag 23 maj 2016 kl. 00:57:57 UTC+2 skrev Michael Haufe (TNO):
> On Sunday, May 22, 2016 at 4:59:50 PM UTC-5, jonas.t...@gmail.com wrote:
> > Den söndag 22 maj 2016 kl. 18:03:24 UTC+2 skrev Michael Haufe (TNO):
> > > On Saturday, May 21, 2016 at 12:41:36 PM UTC-5, jonas.t...@gmail.com wrote:
> > > > I have an object storing integers, many formulas may of course generate the same integer. And for some expressions for example on form a*x^j i would like to store each occurence. Now all my numbers are printed out in a square grid.
> > > 
> > > Don't. It's going to be slower.
> > 
> > Well just storing them within the array can hardly slow it down or?
> 
> Memoization isn't free and your approach would be even less so. I've written a little about this topic in the past:
> 
> <https://thenewobjective.com/blog/2013/01/dynamic-programming-for-great-justice/>

For even greater justice, but a tad slower using base 10 but faster and bigger using bigger base.

<script language="Javascript">

/*ADD TWO VARIABLES USING CHOSEN BASE*/
function naiveAdd(base, arrOne, arrTwo)
{
   addOne=arrOne.slice();
   addTwo=arrTwo.slice(); 
   abigArr = addOne.length;
   asmallArr = addTwo.length;
   addResult = [];
   remainder = 0;
   for (i = 0; i < asmallArr; i ++ )
   {
      
      addOne[i] = addOne[i] + addTwo[i] + remainder;
      if (addOne[i] >= base)
      {  
         
         addOne[i] = addOne[i] - base;
         remainder = 1;
      
      }
      else
      {
         remainder = 0;
      }
   }
   // If strings of equal length but there is a remainder;
   while (remainder == 1 && addOne[i]==(base-1))
   {
      addOne[i] = 0;
      i++;
   }
   if (remainder == 1) {if (isNaN(addOne[i])) addOne[i] = 0; addOne[i] = addOne[i]+1;}
   return addOne;
}

/* COMPARE SIZE OF TWO ARRAYS, IF A < B RETURN TRUE, IF B >= A RETURN FALSE */
function lessThan(A, B)
{
   var AA = A.length;
   var BB = B.length;
   if(AA > BB) return false;
   if(AA < BB) return true;
   // AA = BB, compare indexes from biggest to smallest.
   for(i = AA - 1; i >= 0; i -- )
   {
      if(A[i] < B[i]) return true;
      if(A[i] > B[i]) return false;
   }
   return false;
}

/* ORDER THE ARRAYS SO THE BIGGER IS PASSED AS FIRST ARGUMENT*/
function orderArrayAdd(Abase, A, B)
{
   return lessThan(A, B) ? counterArr = naiveAdd(Abase, B, A) : counterArr = naiveAdd(Abase, A, B);
}

/* GET VALUES FROM INPUT*/
function fetchValues(){
base=document.eval.FBASE.value;
myeval = document.eval.expression.value;
lucasStart = document.eval.start.value;
lStart=parseInt(lucasStart)
lucasScope = document.eval.scope.value;
lScope=parseInt(lucasScope)
}

/* PARSE VALUES FROM INPUT */
function parseToInt(){
if (result = myeval.indexOf("+") != - 1)
   {
      opArr = myeval.split("+");
      operation = "+";
   }
   arrOne = opArr[0].split("").map(Number).reverse();
   arrTwo = opArr[1].split("").map(Number).reverse();
}


function main(){
document.eval.result.value="";
document.eval.timing.value="";
evalStr="";
var out=[];counter=1;
fetchValues();
base=parseInt(base);
parseToInt();
flip=0;
/* TIMER START */
var start = new Date().getTime();
lEnd=lStart+lScope;

while(counter<=lEnd){
out=orderArrayAdd(base,arrOne,arrTwo);
arrOne=arrTwo.slice();
arrTwo=out.slice();
if (counter>=lStart && counter<lEnd) {fib=out.slice();evalStr+=counter+"th"+" "+fib.reverse().join(',')+"\n";}
counter++;
}
/* TIMER END */
var end = new Date().getTime();
var time = end - start;
document.eval.result.value+=evalStr;
document.eval.timing.value+=time;
} </script>

<html><body onLoad="main()";>
<H1>FIBONACCI AND OTHER SERIES</H1>
<form name="eval" onsubmit="main(); return false;">
CHOSE BASE <input type="text" name="FBASE"  value="4503599627370495" size="20"><P>
<input type="submit" value="Generate"><input type="text" name="timing"  value="" size="4"> ms<br>
Start print x'th number in Serie<input type="text" name="start" value="100000" size="9"> How many follwing in Serie -><input type="text" name="scope" value="1" size="9"><br>
Generate Serie expansons using startvalues<input type="text" name="expression" value="1+0" size="10"><br>
Result <textarea name="result" cols="100" rows="30"></textarea><br>


</form>
</body></html>

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Layered object jonas.thornvall@gmail.com - 2016-05-21 10:41 -0700
  Re: Layered object Aleksandro <aleksandro@gmx.com> - 2016-05-21 19:41 -0400
    Re: Layered object jonas.thornvall@gmail.com - 2016-05-21 23:42 -0700
      Re: Layered object jonas.thornvall@gmail.com - 2016-05-22 03:50 -0700
  Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-22 09:03 -0700
    Re: Layered object jonas.thornvall@gmail.com - 2016-05-22 14:59 -0700
      Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-22 15:57 -0700
        Re: Layered object jonas.thornvall@gmail.com - 2016-05-22 22:46 -0700
          Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-25 17:13 -0700
        Re: Layered object jonas.thornvall@gmail.com - 2016-05-22 23:05 -0700
          Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-25 17:15 -0700
        Re: Layered object jonas.thornvall@gmail.com - 2016-05-22 23:08 -0700
        Re: Layered object jonas.thornvall@gmail.com - 2016-05-23 08:31 -0700
          Re: Layered object jonas.thornvall@gmail.com - 2016-05-24 05:38 -0700
            Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-25 17:22 -0700
              Re: Layered object jonas.thornvall@gmail.com - 2016-05-25 22:56 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-25 23:07 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-25 23:10 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-25 23:24 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 01:47 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 01:53 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 02:58 -0700
                Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-26 06:42 -0700
                Re: Layered object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-26 11:30 +0100
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 03:47 -0700
                Re: Layered object Tim Streater <timstreater@greenbee.net> - 2016-05-26 12:43 +0100
                Re: Layered object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-26 14:13 +0100
                Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-26 07:05 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 03:51 -0700
                Re: Layered object jonas.thornvall@gmail.com - 2016-05-26 04:00 -0700
                Re: Layered object Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-05-27 22:14 +0100
                Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-26 06:40 -0700
          Re: Layered object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-05-25 17:21 -0700

csiph-web