-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuissanceRapide.java
More file actions
43 lines (38 loc) · 985 Bytes
/
Copy pathPuissanceRapide.java
File metadata and controls
43 lines (38 loc) · 985 Bytes
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
import static java.lang.Math.floor;
import java.util.ArrayList;
/**
*
* @author yt646712
*/
public class PuissanceRapide {
public static double PuisR(double a, double n){
if (n != 0){
if (n % 2 == 0){
return PuisR(a, floor(n/2)) * PuisR(a, floor(n/2));
}
else{
return PuisR(a, floor(n/2)) * PuisR(a, floor(n/2)) * a;
}
}
else{
return 1;
}
}
public static int PuisR_2 (int a, int n){
ArrayList<Integer> al = new ArrayList<Integer>();
while(n > 0){
al.add(n % 2);
n = n/2;
}
int nombre = 1;
for (int i = 0; i < al.size(); i++){
if (al.get(i) == 1){
nombre = nombre * nombre * a;
}
if (al.get(i) == 0){
nombre = nombre * nombre;
}
}
return nombre;
}
}