HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Big Integer Math Script/Calculation

07-20-2005, 09:01 PM#1
MSTR
I'm looking for a script that can handle calculating 64 ^ 20 at most and atleast 64^10. I need it for a encryption scheme I am designing. My Calculator can atleast handle it, lol. My encryption method will only work and be REALLY USEFUL if it can handle REALY BIG NUMBERS, or it will be somewhat useful.
07-20-2005, 10:51 PM#2
Zoxc
You can use Pow to calculate 64 ^ 20 like this: R2I(Pow(64, 20)) its a real function.
07-20-2005, 11:06 PM#3
curi
64 is 2^6 (2^6) ^ 20 = 2^120 ints are 32 bit, right? the obvious thing to do big math would be a string of 1's and 0's. string max length is 256 or so i think so that's enough. if it wasn't u could encode multiple bits per character.

actually making many unique strings will give you memory problems. so strings might be a bad idea. you could come up with a format for storing a number in multiple ints. wouldn't be too hard really.

i dunno if there is already code written to do this, so sorry not to be much help.
07-23-2005, 03:12 AM#4
MSTR
Quote:
Originally Posted by curi
i dunno if there is already code written to do this, so sorry not to be much help.

There is, i have one for C++, my teacher gave it too me, i didnt have much time, so i copied it down fast, so there might be some errors on it.

I tried doing 64 * 64 * ... etc.
and power

and got a rounded value

Code:
#include <iostream>
using namespace std;

  typedef short Elem;
  class DLink
  {
    public:
      Elem element;
      DLink * prev;
      DLink * next;
      DLink(const Elem & clemval, DLink * preval = NULL, DLink * nextval = NULL
      {
        element = elemval;
	prev = preval;
	next =  nextval;
      }
      DLink( DLink * preval = NULL, DLink * nextval = NULL)
      {
        prev = preval;
	next = nextval;
      }
  }

  class Longno
  {
    private:
      into Dgtcnt;
      DLink * Head;
      DLink * Tail;
    public:
      Longno()
      {
        Dgtcnt = 0;
	Head = Tail = new DLink;
	Head->prev = Head;
      Head->next = Head;
      }
      ~Longno();
      void zerosup();
      Longno( const Longno &LN );
      Longno & operator = ( const Longno & LN) const;
      Longno operator = + ( const Longno & LN) const;
      Longno operator = - ( const Longno & LN) const;
      bool operator == ( const Longno & LN) const;
      bool operator > ( const Longno & LN) const;
      bool operator < ( const Longno & LN) const;
      bool operator != ( const Longno & LN) const;
      bool operator >= ( const Longno & LN) const;
      bool operator <= ( const Longno & LN) const;
      Longno mult1 ( int d );
      friend ostream & operator << ( ostream & OS, const Longno & LN );
      friend istream & operator >> ( i stream & IST, Longno & LN);
    }
    Longno::Longno( const Longno & LN )
    {
      DLink * p; DLink * q;
      Dgtcnt = LN.Dgtcnt;
      Head = Tail = new DLink;
      Head->prev = Head;
      Head->next = Head;
      q = LN.Head->next;
      while(q != LN.Head )
      {
        p = new DLink;
	p->element = q->element;
	p->prev = Tail;
	p->next = Head;
	Tail->next = p;
	Head->pev = p;
	Tail = p;
	q = q->next;
      }
    Longno & Longno::operator = ( const Longno & LN )
    {
      DLink * p;
      DLink * q;
      Dgtcnt = LN.Dgtcnt;
      if ( this == &LN) return * this;
      p = Head->next;
      while( p != Head)
      {
        p->next->prev = p->prev;
	p->prev->next = p->next;
	delete p;
	p = Head->next;
      }
      Tail = Head;
      q = LN.Head->next;
      while ( q != LN.Head )
      {
        p = new DLink;
	p->element = q->element;
	p->prev = Tail;
	p->next = Head;
	Tail->next = p;
	Head->prev = p;
	Tail = p;
	q = q->next;
      }
        return * this;
      }
	ostream & operator << ( ostream & OS, const Longno & LN )
	{
	  OS << "The number of digits is: " << LN.Dgtcnt << " The digits follow: \n"l
	  DLink * p = LN.Head->next;
	  while ( p != LN.Head )
	  {
	    OS << p->element;
	    p = p->next;
	  }
	OS << endl;
	return OS;  
	}
      istream & operator >> ( istream & IST, Longno & LN )
      {
        char ch; intd;
	LN.Dgtcnt = 0;
	DLink * p = LN.HEAD->next;
	while ( p != LN.Head )
	{
	  p->next->prev = p->prev;
	  p->prev->next = p->next;
	  delete p;
	  p = Head->next;
	}
	LN.Tail = Head;
	cout << "Enter digits. Use minus sign, "-", to stop\n";
	while ( ( IST >> ch ) && ( ch != '-') )
	{
	  if ( ( '0' <= ch ) && ( ch <= '9' ))
	  {
	    d = int ( ch ) - int ( '0' );
	    p = new DLink ( d, NULL, NULL );
	    p->next = LN.Head;
	    p->prev = LN.Tail;
	    LN.Tail->next = p;
	    LN.Head->prev = p;
	  }
	  else
	  {
	    cout << "Enter only digits or minus!\n";
	  }
	}
	zerosup();
	return IST;
      }
      Longno::~Longno()
      {
        DLink * p = Head->next;
	while( p != Head )
	{
	  p->next->prev = p->prev;
	  p->prev->next = p->next;
	  delete p;
	  p = Head->next;
	  delete Head;
	}
      }
    }
07-23-2005, 03:53 PM#5
curi
lol i meant already written in JASS.
07-28-2005, 05:16 PM#6
MSTR
WOW, i banged my head and then i got a idea... on how to handle number big as 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000

MathCraft - Designed to do your math!
http://www.wc3campaigns.com/thread_view.php?t=77244