求梅森素数程序
发布网友
发布时间:2022-10-03 13:55
我来回答
共5个回答
热心网友
时间:2023-10-10 15:43
2008年8月 发现了第46个梅森素数2^43112609-1。是目前已知的最大素数,它有12978189位
这么大的梅森素数的检验程序,百度这可贴不下
现在分布式网络计算中 用的一种是 西班牙uillermo Ballester Valor 编写的Glacus 程序
代码是 c 的
这是下载页面地址 :
http://www.oxixares.com/pub/glucas/snapshots/
最新版本:
http://www.oxixares.com/pub/glucas/snapshots/glucas-2.9.2-20080916.tar.gz
热心网友
时间:2023-10-10 15:43
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
unsigned int *arrBase;
unsigned int *arrCac;
unsigned int *arrResult;
unsigned int DecNum=1000;
unsigned int NUM;
unsigned int StartPos;
unsigned int StartPos4;
void initArr(unsigned int* arr)
{
_asm{
mov ecx, NUM
mov ebx, arr
_init:
mov dword ptr [ebx],0
add ebx,4
loop _init
}
}
void addArr(unsigned int* arr,unsigned int* arr2)
{
__asm{
mov ecx, NUM
mov esi, arr
mov edi, arr2
mov eax, ecx
shl eax, 2
add esi, eax
add edi, eax
clc
_add:
sub esi, 4
sub edi, 4
mov eax, [edi]
adc [esi],eax
loop _add
}
}
void subArr(unsigned int* arr,unsigned int* arr2)
{
_asm {
mov ecx, NUM
dec ecx
mov esi, arr
mov edi, arr2
clc
_sub:
mov eax, [edi+ecx*4]
sbb [esi+ecx*4],eax
loop _sub
}
}
void mulArr(unsigned int* arr,unsigned int num)
{
_asm{
mov ecx,NUM
mov ebx,ecx
shl ebx,2
add ebx,arr
xor edx,edx
mov edi,num
xor esi,esi
_mul:
sub ebx,4
mov eax,[ebx]
mul edi
add eax,esi
mov [ebx],eax
adc edx,0
mov esi,edx
loop _mul
}
}
void divArr(unsigned int *arr,unsigned int num)
{
__asm{
mov ebx,arr
mov ecx,NUM
xor edx,edx
_div: mov eax,[ebx]
div num
mov [ebx], eax
add ebx, 4
loop _div
}
}
/*
caculate arctan(1/num), and store the result to arr
*/
void arctanX(unsigned int num)
{
unsigned int dd;
unsigned int remain=0;
unsigned int loopN;
dd=(long)num*num;
loopN=(int)(32*NUM*log(2)/log(5));
initArr(arrBase);
initArr(arrCac);
arrBase[0]=1;
divArr(arrBase,num);
StartPos=1;
_asm mov ebx,1
forloopN:
_asm mov eax, StartPos
_asm shl eax, 2
_asm mov StartPos4,eax
_asm mov edi, arrCac
_asm add edi, StartPos4
_asm mov esi, arrBase
_asm add esi, StartPos4
_asm mov ecx, NUM
_asm sub ecx, StartPos
_asm xor edx, edx
_asm test ebx,2
_asm jnz cond2
cond1:
fori1:
_asm mov eax, [esi]
_asm div ebx
addCac:
_asm add [edi], eax
_asm jnc fori11
_asm push edi
addCarry: _asm sub edi, 4
_asm add dword ptr [edi], 1
_asm jc addCarry
_asm pop edi
fori11:
_asm add edi, 4
_asm add esi, 4
_asm loop fori1
_asm jmp divarrBase
cond2:
fori2:
_asm mov eax, [esi]
_asm div ebx
subCac:
_asm sub [edi], eax
_asm jnc fori22
_asm push edi
subCarry:
_asm sub edi, 4
_asm sub dword ptr [edi],1
_asm jc subCarry
_asm pop edi
fori22:
_asm add esi, 4
_asm add edi, 4
_asm loop fori2
divarrBase:
_asm xor eax, eax
_asm xor edx, edx
_asm mov esi, dd
_asm mov edi, arrBase
_asm mov ecx, NUM
forBase:
_asm mov eax,[edi]
_asm div esi
_asm mov [edi], eax
_asm add edi, 4
_asm loop forBase
_asm inc ebx
_asm inc ebx
_asm mov esi, arrBase
_asm mov eax, StartPos
_asm shl eax, 2
_asm add esi, eax
_asm cmp dword ptr [esi],0
_asm jnz forloopN
_asm inc StartPos
_asm mov eax,StartPos
_asm cmp eax, NUM
_asm jb forloopN
}
void pArr(unsigned int arr[])
{
printf("%d.\n",arr[0]);
for(unsigned int i=1;i<NUM;i++){
arr[0]=0;
mulArr(arr,100000);
printf("%05ld",arr[0]);
if(i%20==0) printf("\n");
}
}
unsigned int main()
{
unsigned int x;
unsigned int i;
unsigned int j;
unsigned int buf[10];
unsigned int tmp;
FILE* fp;
time_t timerStart, timerEnd;
printf("-----caculating pi--------\n");
printf("input nums of pi:");
scanf("%d",&DecNum);
NUM= (int)(DecNum / log10(2) / 32) + 4;
arrBase=(unsigned int*)calloc(NUM,4);
arrCac=(unsigned int*)calloc(NUM,4);
arrResult=(unsigned int*)calloc(NUM,4);
if(arrBase==NULL || arrCac==NULL || arrResult==NULL)
{
printf("\nerror alloc memory!");
return 9;
}
time(&timerStart);
printf("working... ...");
x=5;
initArr(arrResult);
arctanX(x);
mulArr(arrCac,16);
addArr(arrResult,arrCac);
x=239;
arctanX(x);
mulArr(arrCac,4);
subArr(arrResult,arrCac);
time(&timerEnd);
printf(" ok,time used: %ld\n",timerEnd-timerStart);
fp=fopen("pi.txt","w+");
if(fp==NULL){
printf("error! create file");
return 2;
}
else{
fprintf(fp,"%d.\n",arrResult[0]);
for(i=1;i<=DecNum/5;i++){
arrResult[0]=0;
mulArr(arrResult,100000);
fprintf(fp,"%05d",arrResult[0]);
if(i%20==0) fprintf(fp,"\n");
}
fclose(fp);
}
return 0;
}
热心网友
时间:2023-10-10 15:44
我给你写一个简单的 但效率不高. 你要求的那么大的一个数字很难表示.
Private Sub Command1_Click()
Dim i As Long
Dim j As Long
For i = 1 To 100
For j = i - 1 To 2 Step -1
If i Mod j = 0 Then
Exit For
End If
Next
If j <= 1 Then
For j = 1 To 10 '10000=100*100
If 2 ^ j = i Then
Me.Print i & "是梅森素数"
End If
Next
End If
Next
End Sub
热心网友
时间:2023-10-10 15:44
2^999999是301030位(十进制)的数,这样的数要证明是素数,你只要能证明出一个,估计你就能获得国家科技进步二等奖以上的奖了。
热心网友
时间:2023-10-10 15:45
这个还真不好算.即使是double 型.在进行指数运算的时候也只能表示到2的1024次方.连第15个梅森素数都表示不了.