#include "carac.h"
#include <proto/exec.h>
#include <exec/memory.h>
#include <proto/dos.h>
#include <proto/BigNum.h>
#include <proto/timer.h>
#include <devices/timer.h>
#include <string.h>

#define MEM MEMF_FAST|MEMF_CLEAR
#define TEMPLATE "Number/M,RHO/S,DIFFC/S,BRUTE/S,DeeP/N"
#define TXT GRAS"\n   KillPrime 1.0	The Prime Numbers killer !!"NORM"\n\n   © Allenbrand Brice 1996\n\n   Ringard'Production\n\n   "ITALIK"Usage : KillPrime ?\n\n"NORM
#define WARN "Needs BigNum.library v37 !!\n"
#define VER "$VER: KillPrime 1.0 * "__DATE__
#define PRIME "Probably Prime\n"
#define PIME "Surely Prime\n"
#define UHH "No method !\n"

void maine(void)
{
 struct DosLibrary *DOSBase;
 struct Library *BigNumBase;
 struct RDArgs *rdargs;
 struct Library *TimerBase;
 struct timerequest *tr;
 struct timeval aa,bb;
 static char pipo[]=VER;
 long opts[5];
 int zz=10;
 int a=-5;
 long Err;
 PtrBigNum x,y,z;

 memset(opts,0,sizeof(opts));
 if(DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37))
 {
  if(BigNumBase=OpenLibrary("BigNum.library",37))
  {
   if(tr=(struct timerequest *)AllocVec(sizeof(struct timerequest),MEM))
   {
    if(!(Err=OpenDevice("timer.device",UNIT_MICROHZ,(struct IORequest *)tr,0)))
    {
     TimerBase=(struct Library *)tr->tr_node.io_Device;
     if(rdargs=ReadArgs(TEMPLATE,opts,NULL))
     {
      if(opts[0])
      {
       x=BigNumInit();
       y=BigNumInit();
       z=BigNumInit();
       BigNumStrToBigNum(x,*((char **)opts[0]));
       if(opts[4])
       {
        zz=(*(int *)(opts[1]));
        if(zz>31)
         zz=31;
        if(zz<2)
         zz=2;
       }
       GetSysTime(&aa);
       if(opts[1])
        a=BigNumRho(x,y,zz);
       else
        if(opts[2])
         a=BigNumDiffCarre(x,y,zz);
        else
         if(opts[3])
          a=BigNumBrutePrime(x,1);
       GetSysTime(&bb);
       SubTime(&bb,&aa);
       if(a!=-5)
        Printf("(%ld.%05ld s)\n\n",bb.tv_secs,bb.tv_micro);
       if((!opts[3])||(opts[1])||(opts[2]))
        if(a>0)
         Printf(PRIME);
        else
         if(a==-1)
          Printf(PIME);
         else
          if(!a)
          {
           Printf("=");
           BigNumPrint(y);
           Printf("x");
           BigNumDiv(x,y,z);
           BigNumPrint(z);
           Printf("\n");
          }
          else
           if(!opts[3])
            Printf(UHH);
        BigNumFree(3);
       }
       else
        Printf(TXT);
      FreeArgs(rdargs);
     }
     CloseDevice((struct IORequest *)tr);
    }
    else
     PrintFault(IoErr(),NULL);
    FreeVec(tr);
   }  
   CloseLibrary(BigNumBase);
  }
  else
   Printf(WARN);
  CloseLibrary((struct Library *)DOSBase);
 }
}
