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


Groups > comp.lang.javascript > #27054 > unrolled thread

Bignumb adder completed, work in anybase, any size of number.

Started byjonas.thornvall@gmail.com
First post2015-03-15 07:05 -0700
Last post2015-03-19 11:13 +0100
Articles 6 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  Bignumb adder completed, work in anybase, any size of number. jonas.thornvall@gmail.com - 2015-03-15 07:05 -0700
    Re: Bignumb adder completed, work in anybase, any size of number. jonas.thornvall@gmail.com - 2015-03-17 01:29 -0700
      Re: Bignumb adder completed, work in anybase, any size of number. jonas.thornvall@gmail.com - 2015-03-17 03:05 -0700
        Re: Bignumb adder completed, work in anybase, any size of number. jonas.thornvall@gmail.com - 2015-03-17 06:20 -0700
          Re: Bignumb adder completed, work in anybase, any size of number. jonas.thornvall@gmail.com - 2015-03-19 01:32 -0700
            Re: Bignumb adder completed, work in anybase, any size of number. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-03-19 11:13 +0100

#27054 — Bignumb adder completed, work in anybase, any size of number.

Fromjonas.thornvall@gmail.com
Date2015-03-15 07:05 -0700
SubjectBignumb adder completed, work in anybase, any size of number.
Message-ID<2e3aac17-30fa-4589-8851-96a82748939a@googlegroups.com>
I think it is easily understood code and not to convoluted.
***digit places must be coma separated for any base***
***use decimal notation not base 64***
<SCRIPT LANGUAGE="Javascript">

function naiveAdd(base,arrOne,arrTwo) {
if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.length;}
out="";
remainder=0;
for (i=0;i<length;i++){
one=arrOne[i];
two=arrTwo[i];
one=parseInt(one);
two=parseInt(two);
if (isNaN(one)) one = 0;
if (isNaN(two)) two = 0;
sum=one+two+remainder;

if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
out=","+sum+out;
}
if (remainder==1) out=remainder+out;
return out;
}
base=16;
strOne="2,2";
strTwo="13,13";
arrOne=strOne.split(","); 
arrTwo=strTwo.split(",");
arrOne.reverse();
arrTwo.reverse();
naiveAdd(base,arrOne,arrTwo);
document.write("Sum = ",out,"<BR>");
</SCRIPT>

[toc] | [next] | [standalone]


#27059

Fromjonas.thornvall@gmail.com
Date2015-03-17 01:29 -0700
Message-ID<a51f8107-6650-463a-a6ea-0a55ef524a67@googlegroups.com>
In reply to#27054
Den söndag 15 mars 2015 kl. 15:05:43 UTC+1 skrev jonas.t...@gmail.com:
> I think it is easily understood code and not to convoluted.
> ***digit places must be coma separated for any base***
> ***use decimal notation not base 64***
> <SCRIPT LANGUAGE="Javascript">
> 
> function naiveAdd(base,arrOne,arrTwo) {
> if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.length;}
> out="";
> remainder=0;
> for (i=0;i<length;i++){
> one=arrOne[i];
> two=arrTwo[i];
> one=parseInt(one);
> two=parseInt(two);
> if (isNaN(one)) one = 0;
> if (isNaN(two)) two = 0;
> sum=one+two+remainder;
> 
> if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
> out=","+sum+out;
> }
> if (remainder==1) out=remainder+out;
> return out;
> }
> base=16;
> strOne="2,2";
> strTwo="13,13";
> arrOne=strOne.split(","); 
> arrTwo=strTwo.split(",");
> arrOne.reverse();
> arrTwo.reverse();
> naiveAdd(base,arrOne,arrTwo);
> document.write("Sum = ",out,"<BR>");
> </SCRIPT>

Better or worse?
Is there a way to get rid of leading zeros in array, i am not even sure how they get there?

<SCRIPT LANGUAGE="Javascript">

function naiveAdd(base,arrOne,arrTwo) {
remainder=0;
for (i=0;i<length;i++){
         sum=arrOne[i]+arrTwo[i]+remainder;
         if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
         result[i]=sum;
    }
if (remainder==1) result[i++]=1;
}

function strPrep(strOne,strTwo) {
out="";
arrOne=strOne.split(","); 
arrTwo=strTwo.split(",");
arrOne.reverse();
arrTwo.reverse();
if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.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;
    }
}

/*GLOBAL*/
var arrOne = [];
var arrTwo = [];
var result = [];
length;

/*INPUT*/
base=10;
strOne="9,1";
strTwo="2,0";

/*MAIN*/
j=0;
strPrep(strOne,strTwo);
naiveAdd(base,arrOne,arrTwo);
out=result.reverse().toString();
document.write("Sum = ",out,"<BR>");
</SCRIPT>

[toc] | [prev] | [next] | [standalone]


#27061

Fromjonas.thornvall@gmail.com
Date2015-03-17 03:05 -0700
Message-ID<f4b3f3f8-5227-4443-8f6b-c13c006b2e4b@googlegroups.com>
In reply to#27059
Den tisdag 17 mars 2015 kl. 09:29:20 UTC+1 skrev jonas.t...@gmail.com:
> Den söndag 15 mars 2015 kl. 15:05:43 UTC+1 skrev jonas.t...@gmail.com:
> > I think it is easily understood code and not to convoluted.
> > ***digit places must be coma separated for any base***
> > ***use decimal notation not base 64***
> > <SCRIPT LANGUAGE="Javascript">
> > 
> > function naiveAdd(base,arrOne,arrTwo) {
> > if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.length;}
> > out="";
> > remainder=0;
> > for (i=0;i<length;i++){
> > one=arrOne[i];
> > two=arrTwo[i];
> > one=parseInt(one);
> > two=parseInt(two);
> > if (isNaN(one)) one = 0;
> > if (isNaN(two)) two = 0;
> > sum=one+two+remainder;
> > 
> > if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
> > out=","+sum+out;
> > }
> > if (remainder==1) out=remainder+out;
> > return out;
> > }
> > base=16;
> > strOne="2,2";
> > strTwo="13,13";
> > arrOne=strOne.split(","); 
> > arrTwo=strTwo.split(",");
> > arrOne.reverse();
> > arrTwo.reverse();
> > naiveAdd(base,arrOne,arrTwo);
> > document.write("Sum = ",out,"<BR>");
> > </SCRIPT>
> 
> Better or worse?
> Is there a way to get rid of leading zeros in array, i am not even sure how they get there?
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> function naiveAdd(base,arrOne,arrTwo) {
> remainder=0;
> for (i=0;i<length;i++){
>          sum=arrOne[i]+arrTwo[i]+remainder;
>          if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
>          result[i]=sum;
>     }
> if (remainder==1) result[i++]=1;
> }
> 
> function strPrep(strOne,strTwo) {
> out="";
> arrOne=strOne.split(","); 
> arrTwo=strTwo.split(",");
> arrOne.reverse();
> arrTwo.reverse();
> if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.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;
>     }
> }
> 
> /*GLOBAL*/
> var arrOne = [];
> var arrTwo = [];
> var result = [];
> length;
> 
> /*INPUT*/
> base=10;
> strOne="9,1";
> strTwo="2,0";
> 
> /*MAIN*/
> j=0;
> strPrep(strOne,strTwo);
> naiveAdd(base,arrOne,arrTwo);
> out=result.reverse().toString();
> document.write("Sum = ",out,"<BR>");
> </SCRIPT>


Oops maybe length of array would been preferable.

[toc] | [prev] | [next] | [standalone]


#27062

Fromjonas.thornvall@gmail.com
Date2015-03-17 06:20 -0700
Message-ID<70a18d0d-c3cc-45a4-b96c-0a3a092b2b94@googlegroups.com>
In reply to#27061
Den tisdag 17 mars 2015 kl. 11:05:56 UTC+1 skrev jonas.t...@gmail.com:
> Den tisdag 17 mars 2015 kl. 09:29:20 UTC+1 skrev jonas.t...@gmail.com:
> > Den söndag 15 mars 2015 kl. 15:05:43 UTC+1 skrev jonas.t...@gmail.com:
> > > I think it is easily understood code and not to convoluted.
> > > ***digit places must be coma separated for any base***
> > > ***use decimal notation not base 64***
> > > <SCRIPT LANGUAGE="Javascript">
> > > 
> > > function naiveAdd(base,arrOne,arrTwo) {
> > > if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.length;}
> > > out="";
> > > remainder=0;
> > > for (i=0;i<length;i++){
> > > one=arrOne[i];
> > > two=arrTwo[i];
> > > one=parseInt(one);
> > > two=parseInt(two);
> > > if (isNaN(one)) one = 0;
> > > if (isNaN(two)) two = 0;
> > > sum=one+two+remainder;
> > > 
> > > if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
> > > out=","+sum+out;
> > > }
> > > if (remainder==1) out=remainder+out;
> > > return out;
> > > }
> > > base=16;
> > > strOne="2,2";
> > > strTwo="13,13";
> > > arrOne=strOne.split(","); 
> > > arrTwo=strTwo.split(",");
> > > arrOne.reverse();
> > > arrTwo.reverse();
> > > naiveAdd(base,arrOne,arrTwo);
> > > document.write("Sum = ",out,"<BR>");
> > > </SCRIPT>
> > 
> > Better or worse?
> > Is there a way to get rid of leading zeros in array, i am not even sure how they get there?
> > 
> > <SCRIPT LANGUAGE="Javascript">
> > 
> > function naiveAdd(base,arrOne,arrTwo) {
> > remainder=0;
> > for (i=0;i<length;i++){
> >          sum=arrOne[i]+arrTwo[i]+remainder;
> >          if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
> >          result[i]=sum;
> >     }
> > if (remainder==1) result[i++]=1;
> > }
> > 
> > function strPrep(strOne,strTwo) {
> > out="";
> > arrOne=strOne.split(","); 
> > arrTwo=strTwo.split(",");
> > arrOne.reverse();
> > arrTwo.reverse();
> > if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.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;
> >     }
> > }
> > 
> > /*GLOBAL*/
> > var arrOne = [];
> > var arrTwo = [];
> > var result = [];
> > length;
> > 
> > /*INPUT*/
> > base=10;
> > strOne="9,1";
> > strTwo="2,0";
> > 
> > /*MAIN*/
> > j=0;
> > strPrep(strOne,strTwo);
> > naiveAdd(base,arrOne,arrTwo);
> > out=result.reverse().toString();
> > document.write("Sum = ",out,"<BR>");
> > </SCRIPT>
> 
> 
> Oops maybe length of array would been preferable.
<SCRIPT LANGUAGE="Javascript">
function naiveAdd(base,arrOne,arrTwo) {
remainder=0;
for (i=0;i<length;i++){ 
result[i]=arrOne[i]+arrTwo[i]+remainder; 
if (result[i]>=base) { result[i]=result[i]-base; remainder=1;} 
else {remainder=0;} 
} 
if (remainder==1) result[i++]=1; else result.splice(i, 1);;
}
function strPrep(strOne,strTwo) {
arrOne=strOne.split(",").reverse(); 
arrTwo=strTwo.split(",").reverse();
if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.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;
}
}
/*GLOBAL*/
var arrOne = [];
var arrTwo = [];
var result = [];
length;
/*INPUT*/
base=2;
strOne="1,1";
strTwo="1,0";
/*MAIN*/
j=0;
strPrep(strOne,strTwo);
naiveAdd(base,arrOne,arrTwo);
out=result.reverse().toString();
document.write("Sum = ",out,"<BR>");
</SCRIPT>

[toc] | [prev] | [next] | [standalone]


#27072

Fromjonas.thornvall@gmail.com
Date2015-03-19 01:32 -0700
Message-ID<d086e346-45ed-45c5-aa12-1b3da305569d@googlegroups.com>
In reply to#27062
Den tisdag 17 mars 2015 kl. 14:20:13 UTC+1 skrev jonas.t...@gmail.com:
> Den tisdag 17 mars 2015 kl. 11:05:56 UTC+1 skrev jonas.t...@gmail.com:
> > Den tisdag 17 mars 2015 kl. 09:29:20 UTC+1 skrev jonas.t...@gmail.com:
> > > Den söndag 15 mars 2015 kl. 15:05:43 UTC+1 skrev jonas.t...@gmail.com:
> > > > I think it is easily understood code and not to convoluted.
> > > > ***digit places must be coma separated for any base***
> > > > ***use decimal notation not base 64***
> > > > <SCRIPT LANGUAGE="Javascript">
> > > > 
> > > > function naiveAdd(base,arrOne,arrTwo) {
> > > > if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.length;}
> > > > out="";
> > > > remainder=0;
> > > > for (i=0;i<length;i++){
> > > > one=arrOne[i];
> > > > two=arrTwo[i];
> > > > one=parseInt(one);
> > > > two=parseInt(two);
> > > > if (isNaN(one)) one = 0;
> > > > if (isNaN(two)) two = 0;
> > > > sum=one+two+remainder;
> > > > 
> > > > if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
> > > > out=","+sum+out;
> > > > }
> > > > if (remainder==1) out=remainder+out;
> > > > return out;
> > > > }
> > > > base=16;
> > > > strOne="2,2";
> > > > strTwo="13,13";
> > > > arrOne=strOne.split(","); 
> > > > arrTwo=strTwo.split(",");
> > > > arrOne.reverse();
> > > > arrTwo.reverse();
> > > > naiveAdd(base,arrOne,arrTwo);
> > > > document.write("Sum = ",out,"<BR>");
> > > > </SCRIPT>
> > > 
> > > Better or worse?
> > > Is there a way to get rid of leading zeros in array, i am not even sure how they get there?
> > > 
> > > <SCRIPT LANGUAGE="Javascript">
> > > 
> > > function naiveAdd(base,arrOne,arrTwo) {
> > > remainder=0;
> > > for (i=0;i<length;i++){
> > >          sum=arrOne[i]+arrTwo[i]+remainder;
> > >          if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
> > >          result[i]=sum;
> > >     }
> > > if (remainder==1) result[i++]=1;
> > > }
> > > 
> > > function strPrep(strOne,strTwo) {
> > > out="";
> > > arrOne=strOne.split(","); 
> > > arrTwo=strTwo.split(",");
> > > arrOne.reverse();
> > > arrTwo.reverse();
> > > if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.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;
> > >     }
> > > }
> > > 
> > > /*GLOBAL*/
> > > var arrOne = [];
> > > var arrTwo = [];
> > > var result = [];
> > > length;
> > > 
> > > /*INPUT*/
> > > base=10;
> > > strOne="9,1";
> > > strTwo="2,0";
> > > 
> > > /*MAIN*/
> > > j=0;
> > > strPrep(strOne,strTwo);
> > > naiveAdd(base,arrOne,arrTwo);
> > > out=result.reverse().toString();
> > > document.write("Sum = ",out,"<BR>");
> > > </SCRIPT>
> > 
> > 
> > Oops maybe length of array would been preferable.
> <SCRIPT LANGUAGE="Javascript">
> function naiveAdd(base,arrOne,arrTwo) {
> remainder=0;
> for (i=0;i<length;i++){ 
> result[i]=arrOne[i]+arrTwo[i]+remainder; 
> if (result[i]>=base) { result[i]=result[i]-base; remainder=1;} 
> else {remainder=0;} 
> } 
> if (remainder==1) result[i++]=1; else result.splice(i, 1);;
> }
> function strPrep(strOne,strTwo) {
> arrOne=strOne.split(",").reverse(); 
> arrTwo=strTwo.split(",").reverse();
> if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.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;
> }
> }
> /*GLOBAL*/
> var arrOne = [];
> var arrTwo = [];
> var result = [];
> length;
> /*INPUT*/
> base=2;
> strOne="1,1";
> strTwo="1,0";
> /*MAIN*/
> j=0;
> strPrep(strOne,strTwo);
> naiveAdd(base,arrOne,arrTwo);
> out=result.reverse().toString();
> document.write("Sum = ",out,"<BR>");
> </SCRIPT>

https://www.youtube.com/watch?v=8Y0yolPv7tU

[toc] | [prev] | [next] | [standalone]


#27074

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-03-19 11:13 +0100
Message-ID<XnsA462721B3D399eejj99@194.109.6.166>
In reply to#27072
jonas.thornvall@gmail.com wrote on 19 mrt 2015 in comp.lang.javascript:

>> /*MAIN*/
>> j=0;
>> strPrep(strOne,strTwo);
>> naiveAdd(base,arrOne,arrTwo);
>> out=result.reverse().toString();
>> document.write("Sum = ",out,"<BR>");
>> </SCRIPT>
> 
> https://www.youtube.com/watch?v=8Y0yolPv7tU

<https://youtu.be/w4sfZrVZM7I>

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web