-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFermatPrimality.java
More file actions
executable file
·66 lines (63 loc) · 2.01 KB
/
FermatPrimality.java
File metadata and controls
executable file
·66 lines (63 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package crypto.projet;
/**
** Programme Java pour implémenter l'algorithme de test de primalité de Fermat
**/
import java.util.Scanner;
import java.util.Random;
/** La Classe FermatPrimality **/
public class FermatPrimality
{
/** Fonction pour vérifier si le nombre est premier ou pas **/
public boolean isPrime(long n, int iteration)
{
/** cas de base **/
if (n == 0 || n == 1)
return false;
/** cas de base - 2 est premier **/
if (n == 2)
return true;
/** Un nombre pair autre que 2 est composé **/
if (n % 2 == 0)
return false;
Random rand = new Random();
for (int i = 0; i < iteration; i++)
{
long r = Math.abs(rand.nextLong());
long a = r % (n - 1) + 1;
if (modPow(a, n - 1, n) != 1)
return false;
}
return true;
}
/** Fonction pour calculer (a ^ b) % c **/
public long modPow(long a, long b, long c)
{
long res = 1;
for (int i = 0; i < b; i++)
{
res *= a;
res %= c;
}
return res % c;
}
/** Fonction Main **/
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Test d'Algorithme de Primalité de Fermat\n");
/** Faire un objet de la classe FermatPrimality **/
FermatPrimality fp = new FermatPrimality();
/** Accepter le numéro **/
System.out.println("Entrer un nombre\n");
long num = scan.nextLong();
/** Accepter le nombre d'itérations **/
System.out.println("\nEntrer le nombre d'itérations");
int k = scan.nextInt();
/** Vérifie si c'est premier **/
boolean prime = fp.isPrime(num, k);
if (prime)
System.out.println("\n"+ num +" est premier");
else
System.out.println("\n"+ num +" est composé");
}
}