home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
vol_200
/
247_01
/
brute.c
< prev
next >
Wrap
Text File
|
1989-04-19
|
1KB
|
49 lines
/*
* Program to factor numbers using brute force.
*/
#include <stdio.h>
#include "miracl.h"
#define LIMIT 15000
main()
{ /* find factors by brute force division */
big x,y;
int n,p;
int *primes;
mirsys(50,MAXBASE);
x=mirvar(0);
y=mirvar(0);
primes=gprime(LIMIT);
n=0;
p=primes[0];
printf("input number to be factored\n");
cinnum(x,stdin);
if (prime(x))
{
printf("this number is prime!\n");
exit(0);
}
printf("factors are \n");
while (size(x)>=p)
{ /* try division by each prime in turn */
if (p==0)
{ /* run out of primes */
printf("composite factor ");
cotnum(x,stdout);
exit(0);
}
if (subdiv(x,p,y)==0)
{ /* factor found */
copy(y,x);
printf("prime factor ");
printf("%d\n",p);
if (prime(x)) break;
}
else p=primes[++n];
}
printf("prime factor ");
cotnum(x,stdout);
}