入力された値のべき乗を求めるプログラム
脳死で書いたので、もっと早い方法がありそうに思う。
(int)sqrt(num*1.0)も正しく平方根が求められているのか、少し自信がない。
sqrt関数の戻り値が桁落ちして切り捨てされたら正しい値が返ってこないので。
コード
#include <stdio.h> #include <math.h> int main(void) { int num; scanf("%d", &num); for(int m = 2; m <= (int)sqrt(num*1.0); m++){ int current = num; int n = 0; while(current%m == 0){ current /= m; n++; if(current == 1){ printf("%d is %d to the power of %d\n",num,m,n); break; } } } return 0; }
入力
256
出力
256 is 2 to the power of 8 256 is 4 to the power of 4 256 is 16 to the power of 2