素因数分解 Factorize
2007.03.08 11:33 整数
001 void factorize( int x, vector<int> &factors ){ 002 int d, q; 003 while ( x >= 4 && x % 2 == 0 ) { 004 factors.push_back(2); 005 x /= 2; 006 } 007 d = 3; q = x / d; 008 while ( q >= d ){ 009 if ( x % d == 0 ) { 010 factors.push_back(d); 011 x = q; 012 } else { 013 d += 2; 014 } 015 q = x / d; 016 } 017 factors.push_back(x); 018 }
スポンサーサイト
| コメント(0) | トラックバック(0) | ↑ページトップ |