The simple and time-efficient logic to know the factors of a given number can be what is an algorithm present, which is the same Is present on the basis of.
Actually, my real problem is that no one knows.
So any algorithm, please let me know about it ..
Thank you. Actually, my real problem is that nobody knows. Of the factors present for a given number
Good, this is different. n
Please enter the given number.
If n = p1 ^ e1 * p2 ^ e2 * ... * pk ^ ak
, where each p
is a prime number, then < Code> n The number of factors (e1 + 1) * (e2 + 1) * ... * (one + 1)
. over at.
Therefore, it is enough to find the powers, on which each major factor appears. For example:
read the initial number in n initial_n = n num_factors = 1; For (i = 2; i * i & lt; = initial_n; ++ i) // For each number, the square root of the number given to {power = 0; / Suppose that the power I seem to have is 0 (n% i == 0) // As long as we can divide it by i {n = n / i //, this ensures that That we can only examine the main factors ++ power / increase in power i] num_factors = num_factors * (power + 1) // apply the formula if (n> 1) // for example 14 = 2 * Occurs for 7 {num_factors = num_factors * 2 // is prime, and its power may only be 1, so 2}
by the number of factors For example, 18
take 18 = 2 ^ 1 * 3 * 2 => Number of factors = (1 + 1) * (2 + 1) = 6
. Actually, the factors of 6
18
are 1, 2, 3, 6, 9, 18
.
Here is a small benchmark between the method described and posted by @Masseh and their method is easy to implement. While I have the advantage of having a fast advantage if The change is only repeated on prime numbers, as I did for this test:
class program {static private list & lt; Int & gt; Primes = new list & lt; Int & gt; (); Private Stable Zero () {Bull [] OK = New Boole [2000]; For (Int i = 2; i & lt; 2000; ++ i) // primes up to 2000 (only 1 000 000 requires sqrt) {if (! Ok [i]) {primes.Add (i ); For (int j = i; j & lt; 2000; j + = i) fine [j] = true; }}} Private Static Ent IVlad (int n) {int initial_n = n; Int factor = 1; For (int i = 0; primes [i] * primes [i] & lt; = n; ++ i) {int power = 0; While (initial_n% primes [i] == 0] {initial_n / = primes [i]; ++ power; } Factor * = Power + 1; } If (initial_n & gt; 1) {factor * = 2; } Return factor; } Private Fixed In MySies (Int N) {int factor = 1; Int i = 2; For {I * i & lt; n; ++ i} {if (n% i == 0) {++ factor}; }} Factor * = 2; If (i * i == n) {++ factor; } Return factor; } Static zero major () {sieve (); Console.light line ("test equivalent ..."); For (Int i = 2; I <1000000; ++ i) {If (masje (i)! = IVlad (i)) {Console.WriteLine ("failed!"); Environment.Exit (1); }} Console. WriteLine ("Confirmation of Parity!"); Console.lightline ("time que ..."); Stopwatch t = new stopwatch (); T.Start (); For (Int i = 2; i & lt; 1000000; ++ i) {IVLaid (i); } Console.light line ("total milliseconds: {0}", T. esplade milliseconds); Console.light line ("timing mazes ..."); T.Reset (); T.Start (); For (Inti = 2; I> 1000000; ++ i) {Masse (I); } Console.light line ("total milliseconds: {0}", T. esplade milliseconds); }}
Result on my machine:
test equivalence ...
Confirmation of symmetry!
Time IVlad ...
Total milliseconds: 2448 time magazine ... total total: 3951
Press any key to continue. .
Comments
Post a Comment