# modulo on big numbers

• 08-07-2013, 06:58 AM
AppieDijk
modulo on big numbers
Hi,

I've got an PDF form with formfields.
Some of the need to calculate a code, therefore I need a Javascript code...

The code that i've got does work for small numbers but not for big numbers...
I'v got this code:

=====
if(getField("TestIBAN").value == "")
{
event.value = 0;
}
else
{
event.value = (getField("TestIBAN").value % 97);
}
=====

I've found a snippet on the web for big numbers but I don't know how to use them in the code above... Can someone help me?

The snippet:

=====
Max value for Javascript numbers is: 9007199254740992 This function performs a modulo on numbers bigger than that.

"aNumStr" is the number (as string) you want to devide. "aDiv" is the number you want to devide by.

{
var tmp = "";
var i, r;
for ( i=0; i<aNumStr.length ; i++)
{
tmp += aNumStr.charAt( i);
tmp = r.toString( 10);
}
return tmp / 1;
}
=====

Thnx
• 08-07-2013, 07:13 AM
AppieDijk
And the next question I need to make the big number, it's a combination of some fields...

The fields: Bankname (4 characters), Country (2 characters), bankaccount (10 numbers, if less add zero's before number)

First we need to make a number of the 'bankname' (we have to do that by changing all the letters by there position in the Latin alphabet, A=11, B=12, C=13, D=14 .... Z=35

Behind that number we have to put the 10 numbers of the bankaccount
Behind that we have to put the Country in numers (same way as 'bankname')
And at least we need to add 00 (two zero's)

The long number that we've got there is the 'aNumStr' as in the script above... and the 'aDiv' is 97.
Finally we have to calculate '98 - the result of modulo' (1 - 9 need to be shown as 01 - 09).

(as you can see, I need an IBAN calculator)
• 08-07-2013, 09:15 AM
wbport
Remember how you were taught long division as a youngster? Try writing a program to do the same thing. If it will work on smaller numbers you can verify with the modulo operator, it should work on the really big ones.
• 08-07-2013, 04:12 PM
007Julien
With Javascript, Numbers are effectively stored as floating point with a 52 bit mantissa 2^53-1 == 9 007 199 254 740 991 with a 64-bit processor (The bitwise operators and shift operators operate only on 32-bit integers, but % operate always on Numbers with 15 numbers).

If I understand right, You have to work with Strings (representative of Numbers) of : 8 (4 double bank name characters) + 4 (2 country double characters) + 10 (bank account) + 2 (00) = 24 characters.

Then you have only to cut your String in 2 pieces of 12 characters and write :

'aNumStr' = '12firstNumStrChars' * 1,000,000,000,000 + '12lastNumStrChars';

'aNumStr'%97 = ( ('12firstNumStrChars'%97)* (1,000,000,000,000%97) + '12lastNumStrChars'%97 ) % 97;

But 1,000,000,000,000 % 97 is 50. Then your function is simply
Code:

```String.prototype.bankAccountKey=function(){return ((this.substr(0,12)%97)*50+this.substr(12)%97)%97;} // Test alert(1000000000000%97); // => 50 // Use var aNumStr='012345678901234567890123'; // Use alert(aNumStr.bankAccountKey()) // => 19 </script>```
• 08-07-2013, 05:33 PM
007Julien
Obviously (n*97+r)*(m*97+s) is egal to (n*m+r*m+s*n)*97 + (r*s) then is rest % 97 is (r*s) % 97 !
Then (A*B)%97 is ((A%97)*(B%97))%97

The function is to complete with 98 - the result and a leading zero
String.prototype.bankAccountKey=function(){
var m=98-((this.substr(0,12)%97)*50+this.substr(12)%97)%97;
return m<10?'0'+m:''+m;
}
• 08-07-2013, 06:50 PM
007Julien
But the order of terms seems not the same as yours see wikipedia

A new code for any string length with Wikipedia example

Code:

```var aNumStr='3214282912345698765432161182';   String.prototype.bankAccountKey=function(){         var str=this.toString(),deb,rst;         while (deb=str.substr(0,12),str=str.substr(12)) {str=(deb%97).toString()+str;}         rst=98-deb%97;         return rst<10?'0'+rst:''+rst; } alert(aNumStr.bankAccountKey())```
• 08-07-2013, 11:47 PM
Quote:

Originally Posted by 007Julien
Obviously (n*97+r)*(m*97+s) is egal to (n*m+r*m+s*n)*97 + (r*s)

No, it is not. Luckily the argument still works with the correct result.
• 08-08-2013, 03:36 AM
007Julien
You are rigth Airblader. I forgott a 97 with the term n*m (which is to replace by n*m*97), this do'not change the result which is k*97+(r*s). But this formula is not the best to calculate a modulo for a number in form of a big string.

The rule is that, it's possible to replace a n length « string number » from left by is result %97.
With 32 bit systems and a largest integer 10 digits length 2 147 483 647), it's probably secure to take 9 length strings and to use this prototype :

Code:

```String.prototype.bankAccountKey=function(){var s=this;     //Some transformations of the chain     //...     while (2<s.length) s=''+(s.substr(0,9)%97)+s.substr(9)     s=98-s;     return s<10?'0'+s:''+s; } // to replace spaces and letters  (A=>10, B=>11 etc z=>36) var str='W E S T12345698765432 GB82'; var rsp=str.replace(/\s/g,'').toUpperCase().replace(/[A-Z]/g,function(a){return a.charCodeAt(0)-55}) alert(str+'\n'+rsp)```
It is sometimes complicated to make it simple !
• 08-12-2013, 04:51 PM
Ayşe
Code:

```  <script type="text/javascript"> var s = '3214282912345698765432161182';  // 1 //var s='012345678901234567890123';      // 19 //var s = '1000000000000';              // 50 var N = [ s.slice(0, 9) % 97 ]; for(var i=9; i< s.length; i+=7) { // alert( '\n  N[0] + s.slice(i, i+7) =  '+  N[0] + s.slice(i, i+7) ); N[0] = ( N[0] + s.slice(i, i+7) ) % 97; // alert('i nin değeri = '+i+ ' \nDöngüdeki N = '+ N ); } alert('N = '+ N); </script>```

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.13653 seconds
• Memory Usage 2,380KB
• Queries Executed 11 (?)
Template Usage (21):
• (4)bbcode_code_printable
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates