0% found this document useful (0 votes)
18 views2 pages

34

The document is a C program that implements a Fermat primality test to check if a given odd integer n is prime. It prompts the user for input, generates random numbers, and calculates a value r using modular exponentiation. The program iterates a specified number of times to verify the primality of n, outputting whether n is a prime number or a composite number based on the results.

Uploaded by

hoanglee1148
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

34

The document is a C program that implements a Fermat primality test to check if a given odd integer n is prime. It prompts the user for input, generates random numbers, and calculates a value r using modular exponentiation. The program iterates a specified number of times to verify the primality of n, outputting whether n is a prime number or a composite number based on the results.

Uploaded by

hoanglee1148
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<stdio.

h>
#include<math.h>
#include<windows.h>
//TT fermat
int main()
{
int n,t,a,ran=0,i,m;
label:
printf("\nNhap n :"); scanf("%d",&n);
m=n;
if((n<3)|(n%2==0))
{
printf("\nSo n le va lon hon hoac bang 3, xin moi nhap lai!\n");
goto label;
}
printf("\nSo lan lap:"); scanf("%d",&t);
for(i=1;i<=t;i++)
{
printf("\n----------------\nLan lap %d",i);
while(ran>=0)
{
ran=rand();
if((ran>=2)&&(ran<=n-2)) break;
}
a=ran;
printf("\n ran = %d",a);
//da co n, so random a, tinh r = a^(n-1) mod n
//tinh r bang pp nhan bp co lap
int r,x,A;
x=n-1; //=> tinh r = a^x (mod n)
int b;
b=1; A=a;
if(x==0) //that ra truong hop nay khong the xay ra vi n>=3, x = n-1 >=2
{
printf("\nr = b = 1");
}
//doi x thanh so nhi phan
int nhiphan,k[20],j=0,d;
nhiphan=x;
printf("\n");
while(nhiphan>0)
{
k[j]=nhiphan%2;
nhiphan=(nhiphan-k[j])/2;
printf(" k[%d]=%d",j,k[j]);
j++;
}
if(k[0]==1) b=a; //cai nay la khong the vi n la so le -> x la so chan
for(d=0;d<=j;d++)
{
A=(A*A)%m;
if(k[d]==1)
b=(A*b)%m;
printf("\n %d %d %d",d,A,b);
}
printf("\nr = b= %d",b); //b la r = a^(n-1) mod n
if(b!=1)
printf("\nDay la hop so");
else printf("\nDay l� so nguyen to");
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy