Find the private number

There is a number of pressures N, I need to find out the number of integers for which repetitive divisionwith N it gives a factor value.

For Ex:

N=8
Numbers Are 2 as: 8/2=4/2=2/2=1
            5 as  8/5=1
            6 as  8/6=1 
            7 and 8

My Aprroach: All numbers from N/2+1to Ngive me a coefficient 1, therefore

Ans: N/2 + Check Numbers from (2, sqrt(N))

Time complexity O(sqrt(N))

Are there any better ways to do this since the number can be up 10^12and there can be many requests?

Could it be O(1)or O(40)(because 2 ^ 40 exceeds 10 ^ 12)

+4
source share
4 answers

-, O(sqrt(N)), , . k, ( ) O(log(k)). , N/2 + (log(2) + log(3) + ... + log(sqrt(N)) = O(log(N) * sqrt(N)).

, , . , 1 k , k^t <= N < 2 * k^t t=floor(log_k(N)).

, k^t <= N < 2 * k^(t+1). < .

, t, - , . C(N). , C(2) + C(3) + .... + C(sqrt(N)). log, O(sqrt(N)).

, N = 8:

  • 2^3 <= 8 < 2 * 2^3: 1
  • floor(log_3(8)) = 1 8 3^1 <= 8 < 2 * 3^1: 0
  • floor(log_4(8)) = 1 8 4^1 <= 8 < 2 * 4^1: 0
  • 4 5, 6, 7 8 8 t=1 .

, 3 4, , . , [N/2..N] , , .

, O(sqrt(N)), .

0

O(1) , - naturals 10^12, naturals, , 2 000 000 ; 1 1 000 000, ; 1,000,000...10,001 ; 10,000...1,001 ; , , 40 .

/ (, 512 -> 2, 2^9 8^3).

0

.

- wiki

#include <math.h>
#include <stdio.h>

unsigned long long nn = 0;

unsigned repeat_div(unsigned n, unsigned d) {
  for (;;) {
    nn++;
    unsigned q = n / d;
    if (q <= 1) return q;
    n = q;
  }
  return 0;
}

unsigned num_repeat_div2(unsigned n) {
  unsigned count = 0;
  for (unsigned d = 2; d <= n; d++) {
    count += repeat_div(n, d);
  }
  return count;
}

unsigned num_repeat_div2_NM(unsigned n) {
  unsigned count = 0;
  if (n > 1) {
    count += (n + 1) / 2;
    unsigned hi = (unsigned) sqrt(n);
    for (unsigned d = 2; d <= hi; d++) {
      count += repeat_div(n, d);
    }
  }
  return count;
}

unsigned num_repeat_div2_test(unsigned n) {
  // number of integers that repetitive division with n gives quotient one.
  unsigned count = 0;

  // increment nn per code' tightest loop
  ...

  return count;
}

///

unsigned (*method_rd[])(unsigned) = { num_repeat_div2, num_repeat_div2_NM,
    num_repeat_div2_test};
#define RD_N (sizeof method_rd/sizeof method_rd[0])

unsigned test_rd(unsigned n, unsigned long long *iteration) {
  unsigned count = 0;
  for (unsigned i = 0; i < RD_N; i++) {
    nn = 0;
    unsigned this_count =  method_rd[i](n);
    iteration[i] += nn;
    if (i > 0 && this_count != count) {
      printf("Oops %u %u %u\n", i, count, this_count);
      exit(-1);
    }
    count = this_count;
    // printf("rd[%u](%u)      = %u.  Iterations:%llu\n", i, n, cnt, nn);
  }

  return count;
}

void tests_rd(unsigned lo, unsigned hi) {
  unsigned long long total_iterations[RD_N] = {0};
  unsigned long long total_count = 0;
  for (unsigned n = lo; n <= hi; n++) {
    total_count += test_rd(n, total_iterations);
  }
  for (unsigned i = 0; i < RD_N; i++) {
    printf("Sum rd(%u,%u) --> %llu.  total Iterations %llu\n", lo, hi,
        total_count, total_iterations[i]);
  }
}

int main(void) {
  tests_rd(2, 10 * 1000);
  return 0;
}
0

, 10^12, , 2 to 10^6, 40, , i^(len-1)+ y i 2 10 ^ 6, len - 1 40.

, O(40*Query)

-1

Source: https://habr.com/ru/post/1662867/


All Articles