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


Groups > comp.lang.javascript > #27143

Re: naive Big Numb multiplication work.

Newsgroups comp.lang.javascript
Date 2015-03-29 10:24 -0700
References <97053e48-7ead-4d3f-b8e9-26e9f569c573@googlegroups.com> <24d203f8-88e4-4b81-8bdd-7173076f0f51@googlegroups.com> <g83dhadnudtndnn7rnjf823vd7ikgmgva4@4ax.com> <7f775ce1-b3a5-4a37-9f8d-71cf1e1f7a2b@googlegroups.com>
Message-ID <b0cd5608-93ba-41fd-9ce5-b63ab4e3aeaa@googlegroups.com> (permalink)
Subject Re: naive Big Numb multiplication work.
From jonas.thornvall@gmail.com

Show all headers | View raw


Den lördag 28 mars 2015 kl. 15:20:57 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 28 mars 2015 kl. 12:15:15 UTC+1 skrev John Harris:
> > On Fri, 27 Mar 2015 13:48:35 -0700 (PDT), jonas.thornvall@gmail.com
> > wrote:
> > 
> > >Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
> > >> I can say i had hope for less code, those checks of lenghts ain't pretty.
> > >> 
> > >> <SCRIPT LANGUAGE="Javascript">
> >   <snip>
> > >> </HTML>
> > >
> > >Honestly it is magneficient it totally rocks even in unary base ;)
> > 
> > To complete this project you need to implement mixed radix arithmetic.
> > For instance : 
> >   pounds, shillings, and pence; 
> >   hundredweights, stones, pounds, and ounces; 
> >   yards, feet, and inches.
> > 
> > If you think it's difficult, remember that in Britain before 1970
> > pounds, shillings, and pence arithmetic had to be done by 9 year olds.
> > They were expected to be able to do addition and subtraction, but
> > multiplication and division only by simple, decimal, numbers.
> > 
> >   John
> 
> It may be that i include doing the arithmetic in bijective bases.
> http://jt.node365.se/bijbase.html

<SCRIPT LANGUAGE="Javascript">

/* MULTIPLY TWO VARIABLES */

function naiveMult(base, multOne, multTwo)
{
   // document.calc.comment.value = "[Multiply] " + multOne + sign + multTwo + " \n"
   document.calc.comment.value = "";
   document.calc.comment.value += "FACTOR ONE " + lengthTwo + " digits \n"
   document.calc.comment.value += "FACTOR TWO " + length + " digits \n"

   total = [];
   result = [];
   total[0] = 0;
   for (var i = 0; i < lengthTwo; i ++ )
   {
      multresult = [];
      remainder = 0;
      tempVal = 0;
      tempDig = 0;
      j = 0;

      if(i > 0)
      {
         for(zero = 0; zero < i; zero ++ ) multresult[zero] = 0;
      }

      while(j < length)
      {
         intmult ++ ;
         tempVal = (multOne[i] * multTwo[j]) + remainder;
         remainder = 0;

         if (tempVal >= base)
         {
            tempDig = tempVal % base;
            remainder = (tempVal - tempDig) / base;
            multresult[j + i] = tempDig;
         }
         else
         {
            multresult[j + i] = tempVal;
         }
         j ++ ;
      }

      if(remainder != 0)
      {
         multresult[j + i] = remainder;

      }
      // document.calc.comment.value += "\n SubSUM= " + total;
      // document.calc.comment.value += "\n SubPROD= " + multresult + "\n";
      // KOLLA NOGA
      if (multresult.length >= total.length)
      {
         mlength = multresult.length;
         mlengthTwo = total.length;
      }
      else
      {
         mlength = total.length;
         mlengthTwo = multresult.length;
      }

      for (k = 0; k < mlength; k ++ )
      {
         if (isNaN(multresult[k])) multresult[k] = 0;
         if (isNaN(total[k])) total[k] = 0;
      }
      naiveAdd(base, mlength, total, multresult);
      total = result;
   }

   // document.calc.comment.value += "\nTOTAL PRDDUKT= " + total;
}

/* ADD TWO VARIABLES */

function naiveAdd(base, alength, addOne, addTwo)
{
   // document.calc.comment.value += "\nAdd termOne+termTwo =\n" + addOne + "\n" + addTwo + " + ";
   addResult = [];
   remainder = 0;
   for (i = 0; i < alength; i ++ )
   {
      addResult[i] = addOne[i] + addTwo[i] + remainder;
      intadd ++ ;
      // document.calc.comment.value += "\n subAdd = " + addResult[i];
      if (addResult[i] >= base)
      {
         addResult[i] = addResult[i] - base;
         intadd ++ ;
         remainder = 1;
      }
      else
      {
         remainder = 0;
      }

      // document.calc.comment.value += " DigitValue = " + addResult[i];
      // document.calc.comment.value += " carryDigit = " + remainder;
   }

   if (remainder == 1) addResult[i ++ ] = 1;
   else addResult.splice(i, 1);
   result = addResult;

   return result;
}

/* SUBTRACT TWO VARIABLES */

function naiveSub(base, subOne, subTwo)
{
   result = [];
   for (i = 0; i < length; i ++ )
   {
      j = 0;

      if (subOne[i] < subTwo[i])
      {
         subOne[i] += base;
         j = i;
         while (subOne[j + 1] == 0)
         {
            subOne[j + 1] += base - 1;
            j ++ ;

         }
         subOne[j + 1] += - 1;
      }
      j = 0;
      result[i] = subOne[i] - subTwo[i];

   }

   while (result[length - 1] == 0)
   {
      result.splice(length - 1, 1);
      length += - 1;
   }
}

/* PREPARE STRING PAIR */

function strPrep(sign, strOne, strTwo)
{

   arrOne = strOne.split("");
   arrTwo = strTwo.split("");
   arrOne.reverse();
   arrTwo.reverse();
   if (arrOne.length >= arrTwo.length)
   {
      length = arrOne.length;
      lengthTwo = arrTwo.length;
   }
   else
   {
      length = arrTwo.length;
      lengthTwo = arrOne.length;
   }
   for (i = 0; i < length; i ++ )
   {
      arrOne[i] = parseInt(arrOne[i]);
      arrTwo[i] = parseInt(arrTwo[i]);
      if (isNaN(arrOne[i])) arrOne[i] = 0;
      if (isNaN(arrTwo[i])) arrTwo[i] = 0;
   }
}

/* MULTIPLY Container */

function multWrap(sign, multiply, strBase)
{
   tONE = parseInt(multiply[0]);
   tTWO = parseInt(multiply[1]);
   if(tONE <= tTWO)
   {
      arrOne = multiply[0];
      arrTwo = multiply[1];
   }
   else
   {
      arrOne = multiply[1];
      arrTwo = multiply[0];
   }
   strPrep(sign, arrOne, arrTwo);
   naiveMult(base, arrOne, arrTwo);
   // document.calc.msum.value = sum;
   out = total.reverse().toString();
   document.calc.comment.value += "Integer ADD operations " + intadd + " \n"
   document.calc.comment.value += "Integer MULT operations " + intmult + " \n"
   document.calc.comment.value += "Product digits " + out.length + "\n";
   document.calc.comment.value += out;
}

/* SUBTRACTION Container */

function subWrap(sign, strEval, strBase)
{
   tONE = parseInt(subtract[0]);
   tTWO = parseInt(subtract[1]);
   if(tONE < tTWO)
   {
      strOne = subtract[1];
      strTwo = subtract[0];
   }
   else
   {
      strOne = subtract[0];
      strTwo = subtract[1];
   }

   strPrep(sign, strOne, strTwo);
   naiveSub(base, arrOne, arrTwo);

}

/* ADDITION Container */

function addWrap(sign, strEval, strBase)
{
   strOne = adder[0];
   strTwo = adder[1];
   strPrep(sign, strOne, strTwo);
   naiveAdd(base, length, arrOne, arrTwo);

}

/* PARSE THE STRING */

function parser(strEval, strBase)
{

   sign = "*";
   multiply = strEval.split("*");
   // document.calc.firstval.value = multiply[0];
   // document.calc.secval.value = multiply[1];
   multWrap(sign, multiply, strBase)
   // document.calc.tO.value = strOne;
   // document.calc.tT.value = strTwo;
   // sign = "-";
   // subtract = strEval.split("-");
   // subWrap(sign, subtract, strBase);
   // sign = "+";
   // adder = strEval.split("+");
   // addWrap(sign, adder, strBase);
}

/* [MAIN PROGRAM] */
/* [TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT] */
/* [GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */

function fetchValues()
{
   intmult = 0;
   intadd = 0;
   document.calc.eval.value;
   strEval = document.calc.eval.value;
   strBase = document.calc.formbase.value;
   // document.calc.trash.value = "1";
   parser(strEval, strBase);

}

/* GLOBAL */

var multiply = [];
var subtract = [];
var adder = [];
var arrOne = [];
var arrTwo = [];
// var addOne = [];
// var addTwo = [];
// var subOne = [];
// var subTwo = [];
var total = [];
var result = [];
var sum = [];

length = 0;
strOne = "";
strTwo = "";
base = 10;
temp = "";

</SCRIPT>

<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"/></head>
<body bgcolor="gold" onload="fetchValues()">
<H1>Big numbers</H1><P>
<FORM name=calc onSubmit="fetchValues(); return false;">
BASE <input type="text" name="formbase" value="10" size="4"><input type=submit name="calc" value="Calc"><br>
<textarea name="eval" cols="120" rows="20">9832479873298743298222222222222222222222222222274329873433985439857439448584395784397543754389759843759843589437598437598437587854374347327443275438754398759843754375438758437598435784375437054328478754375447543875435743754375435743857439854398543875438759843759843754375943754375843754375435743754387545875485758745984375843754375438574357437598435743875475957487543857574375439857475845749357435743985437584984375434375987435974398754343985984357498589874375949874987597498754987598749875987459874975987439759475974398754987598749754985494385438594359435943859438543854385438543985943859043859043589043859043590435904385904359043584385439589430589043589430588549385435984395843958439058439058439054839054890548905905439058439058490584390589043589043854305438594385904385045894385430943854903584958495843054385043854854038594085430543859043584390584059458504584985049859048540540985485405843085405845848504584054895980458439054980843439805043854054805804854390584354098588504305809874329847329843298*9832479873298743298743298734339854398574398584395784397543754389759843759843589437598437598437587854374347327443275438754398759843754375438758437598435784375437054328478754375447543875435743754375435743857439854398543875438759843759843754375943754375843754375435743754387545875485758745984375822222222222221133444444444444443754375438574357437598435743875475957487543857574375439857475845749357435743985437584984375434375987435974398754343985984357498589874375949874987597498754987598749875987459874975987439759475974398754987598749754985494385438594359435943859438543854385438543985943859043859043589043859043590435904385904359043584385439589430589043589430588549385435984395843958439058439058439054839054890548905905439058439058490584390589043589043854305438594385904385045894385430943854903584958495843054385043854854038594085430543859043584390584059458504584985049859048540540985485405843085405845848504584054895980458439054980843439805043854054805804854390584354098588504305809874329847329843298</textarea>
<!--
RESULT<input type="text" name="result" value="" size="80"><br>

Trash<input type="text" name="trash" value="" size="80"> <br>
FIRSTVAL<input type="text" name="firstval" value="" size="80"><br>
SECVAL<input type="text" name="secval" value="" size="80"><br>
tONE Small<input type="text" name="tO" value="" size="80"><br>
tTWO Big<input type="text" name="tT" value="" size="80"><br>
Sum<input type="text" name="msum" value="" size="80"><br>
-->
<textarea name="comment" cols="120" rows="20" value=""></textarea>
</FORM>
</body>
</html>

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


Thread

naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-27 09:42 -0700
  Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-27 12:14 -0700
  Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-27 13:48 -0700
    Re: naive Big Numb multiplication work. John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-28 11:15 +0000
      Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-28 07:07 -0700
      Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-28 07:18 -0700
      Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-28 07:20 -0700
        Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-29 10:22 -0700
        Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-29 10:24 -0700
          Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-04-02 13:25 -0700
  Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-04-02 16:24 -0700

csiph-web