⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 brute.c

📁 miracl-大数运算库,大家使用有什么问题请多多提意见
💻 C
字号:
/*
 *   Program to factor numbers using brute force.
 *
 *   Copyright (c) 1988-1997 Shamus Software Ltd.
 */

#include <stdio.h>
#include "miracl.h"
#define LIMIT 15000

int main()
{ /* find factors by brute force division */
    int n,p;

#ifdef MR_NOFULLWIDTH
    miracl *mip=mirsys(50,MAXBASE);
#else  
    miracl *mip=mirsys(50,0);
#endif

    char stack_mem[mr_big_reserve(2,50)];  // 2 bigs, 4 bytes per big...
    big x,y;
       
    memset(stack_mem,0,mr_big_reserve(2,50));
    x=mirvar_mem(stack_mem,0);
    y=mirvar_mem(stack_mem,1);

/* This is an example of allocating bigs x and y from the stack rather than 
   from the heap. The "50" should be the same as that used in the prior call 
   to mirsys(.), and MUST be a constant. There is never a need to explicitly 
   delete bigs allocated from the stack, via a call to mirkill(). Therefore
   do not use for sensitive data as these bigs are not erased, unless you
   do it yourself via a call to zero(.) 

   Allocation from the stack is not particularly recommended, but could be 
   faster if bigs are not too big, and are required as temporary variables
   in an often called subroutine. Allocating from the stack also avoids the 
   possibility of memory leaks. 

   NOTE: A big allocated from the stack has a random initial value

*/
    gprime(LIMIT);
    printf("input number to be factored\n");
    cinnum(x,stdin);
    if (isprime(x))
    {
        printf("this number is prime!\n");
        return 0;
    }
    n=0;
    p=mip->PRIMES[0];
    printf("factors are \n");
    forever
    { /* try division by each prime in turn */
        if (subdiv(x,p,y)==0)
        { /* factor found */
            copy(y,x);
            printf("prime factor     ");
            printf("%d\n",p);
            if (size(x)==1) return 0;
            continue;
        }
        if (size(y)<=p) 
        { /* must be prime */
            printf("prime factor     ");
            cotnum(x,stdout);
            return 0;
        }
        p=mip->PRIMES[++n];
        if (p==0) break;
    }
    if (isprime(x)) printf("prime factor     ");
    else            printf("composite factor ");
    cotnum(x,stdout);
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -