I apologize, but pass to C #. But I can sketch on C ++ :)
So, to calculate the number of divisors - we decompose the number into simple factors; the number of dividers after this is defined as ... as in this answer :) - it is written there.
It is easy to search for dividers — it suffices to check the primes to the square root of a number, and decreasing the number itself by dividing by the found prime divisor for speed. The quick square root is picked up by Warren in "Algorithmic Stunts"; but you can use regular sqrt .
We count for each number once, remembering the value for the previous number.
All, putting it all together -
#include <iostream> #include <iomanip> #include <map> using namespace std; unsigned int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317 }; inline unsigned long isqrt(unsigned long x) { unsigned long x1, g0, g1; if (x <= 1) return x; int s = 1; x1 = x - 1; if (x1 > 0xFFFF) { s = s + 8; x1 >>= 16; } if (x1 > 0xFF) { s = s + 4; x1 >>= 8; } if (x1 > 0xF) { s = s + 2; x1 >>= 4; } if (x1 > 0x3) { s = s + 1; } g0 = 1ll << s; g1 = (g0 +(x>>s)) >> 1; while( g1 < g0) { g0 = g1; g1 = (g0 + (x/g0)) >> 1; } return g0; } int factors(unsigned long m) { map<int,int> fs; for(unsigned int i = 0; m > 1 && primes[i] <= isqrt(m);) { if (m%primes[i]) { ++i; continue; } m /= primes[i]; fs[primes[i]]++; } if (m > 1) fs[m]++; int count = 1; for(auto q: fs) count *= (q.second+1); return count; } int main(int argc, const char * argv[]) { int total = 0, last = 1; for(unsigned long n = 2; n < 100000; ++n) { int count = factors(n); if (last == count) { // cout << (n-1) << " vs " << n << " count = " << count << endl; ++total; } else last = count; } cout << total; }
Counts in milliseconds - see https://ideone.com/QRI81U
Once again, I apologize for C ++, not C # ...
nsuch thatnandn+1have the same number of divisors is infinite ( Roger Heath-Brown (1984) ). If the decomposition of a number into prime factorsProduct p^e(p)known, then the number of divisors of the number isProduct (e(p) + 1). Here is for testing, an example on Python, which generates natural numbers and their prime factors to find the number of divisors for each number and print the number of numbers for whichtau(n) == tau(n+1). - jfsn<10^6is 102093 . - jfs