Skip to content

Instantly share code, notes, and snippets.

@rahman99
Last active January 29, 2017 12:29
Show Gist options
  • Select an option

  • Save rahman99/d7e41529d522d7cc164701e4b2b6a8e3 to your computer and use it in GitHub Desktop.

Select an option

Save rahman99/d7e41529d522d7cc164701e4b2b6a8e3 to your computer and use it in GitHub Desktop.
perpangkatan bilangan dengan looping jumlah biner
int bilangan = 3;
int pangkat = 122;
int result=1;
int n=0;
int hasilPangkat;
while(pangkat > 0){
hasilPangkat = (int)Math.pow(2, n);
if(pangkat % 2 == 0)
hasilPangkat=0;
pangkat = pangkat/2;
result *= Math.pow(bilangan, hasilPangkat);
n++;
//biner
// int bit= pangkat % 2;
// pangkat = pangkat/2;
// System.err.print(bit+" ");
}
System.out.println(result);
/**
* sample case: convert number 1292 to basic ten.
* ans: 1000 + 200 + 90 + 2
* else case: number 10200
* ans: 10000 + 200
*/
private static String expandForm(int num) {
int ten = 1;
String result = "";
while (num != 0) {
int rem = num % 10;
if (rem != 0)
result = " + " + (rem * ten) + result;
num = num / 10;
ten = ten * 10;
}
return result.substring(3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment