Methods to Take a Quantity and Sum It’s Digits Raied to the Consecutive Powers in C

The problem

The quantity 89 is the primary integer with a couple of digit that fulfills the valuables in part offered within the identify of this problem. What’s using announcing “Eureka”? As a result of this sum offers the similar quantity.

In impact: 89 = 8^1 + 9^2

The following quantity in having this belongings is 135.

See this belongings once more: 135 = 1^1 + 3^2 + 5^3

We’d like a serve as to gather those numbers, that can obtain two integers a, b that defines the variability [a, b] (inclusive) and outputs a listing of the looked after numbers within the vary that fulfills the valuables described above.

Let’s see some circumstances (enter -> output):

1, 10 -> [1, 2, 3, 4, 5, 6, 7, 8, 9]

1, 100 -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 89]

If there aren’t any numbers of this sort within the vary [a, b] the serve as must output an empty checklist.

90, 100 --> []

The answer in C

Choice 1:

#come with <stddef.h>
#come with <math.h>

typedef unsigned lengthy lengthy ull;

ull *sum_dig_pow(ull a, ull b, ull *effects, size_t *duration) {
    int position = 0;
    for (int i = a; i <= b; i++) {
        int ok = 0;
        ok = log10(i) + 1;
        if (pow((i % 10), ok) > i) i = i + (10 - (i % 10));
        else {
            int op = i;
            int sum = 0;
            for (int m = ok; m > 0; m--) {
                sum += pow(op % 10, m);
                op /= 10;
            }
            if (sum == i) {
                effects[place] = i;
                position++;
            }
        }
    }
    *duration = position;
    go back effects;
}

Choice 2:

#come with <stddef.h>

typedef unsigned lengthy lengthy ull;

ull *sum_dig_pow(ull a, ull b, ull *r, size_t *rl)
{
  // A032799
  static const ull A[20] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175, 518, 598, 1306, 1676, 2427, 2646798, 12157692622039623539ull};
  size_t i;
  
  for(*rl = i = 0; i < 20 && A[i] <= b; i++) {
    if(a <= A[i]) { r[(*rl)++] = A[i]; }
  }
  
  go back r;
}

Choice 3:

#come with <stdio.h>
#come with <string.h>
#come with <math.h>
#come with <stdlib.h>

typedef unsigned lengthy lengthy ull;

ull *sum_dig_pow (ull a, ull b, ull *effects, size_t *duration) {
  char str [snprintf (NULL, 0, "%llu", b) + 1];
  
  for (*duration = 0; a <= b; a++) {
    sprintf (str, "%llu", a);
    
    ull sum = 0;
    for (size_t digit = 0; digit < strlen (str); digit++)
      sum += (ull) pow (*(str + digit) - '0', digit + 1);
    
    if (sum == a)
      *(effects + (*duration)++) = a;
  }
  
  go back effects;
}

Check circumstances to validate our resolution

#come with <criterion/criterion.h>
#come with <stddef.h>
#come with <stdio.h>

typedef unsigned lengthy lengthy ull;

ull *sum_dig_pow(ull a, ull b, ull *effects, size_t *duration);
void tester(ull a, ull b, size_t e_len, ull anticipated[e_len]);

Check(sum_dig_pow, Sample_Tests) {
  {
    ull a = 1; ull b = 10;
    const ull anticipated[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    tester(a, b, 9, (ull *)anticipated);
  }
  {
    ull a = 1; ull b = 100;
    const ull anticipated[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 89};
    tester(a, b, 10, (ull *)anticipated);
  }
  {
    ull a = 10; ull b = 100;
    const ull anticipated[1] = {89};
    tester(a, b, 1, (ull *)anticipated);
  }
  {
    ull a = 90; ull b = 100;
    const ull *anticipated = NULL;
    tester(a, b, 0, (ull *)anticipated);
  }
  {
    ull a = 90; ull b = 150;
    const ull anticipated[1] = {135};
    tester(a, b, 1, (ull *)anticipated);
  }
  {
    ull a = 50; ull b = 150;
    const ull anticipated[2] = {89, 135};
    tester(a, b, 2, (ull *)anticipated);
  }
  {
    ull a = 10; ull b = 150;
    const ull anticipated[2] = {89, 135};
    tester(a, b, 2, (ull *)anticipated);
  }
}

void tester(ull a, ull b, size_t exp_len, ull anticipated[exp_len]) {
    ull effects[exp_len];
    for(size_t i=0; i<exp_len; i++) {
        effects[i] = rand() - rand();
    }
    size_t sub_len = 0;
    ull *submitted = sum_dig_pow(a, b, (ull *)effects, &sub_len);
    if(sub_len != exp_len) {
        cr_assert_fail(
            "< Fallacious Output Period >n nSubmitted: %zunExpected:  %zun n",
                                             sub_len,        exp_len
        );
    }
    for(size_t i=0; i<exp_len; i++) {
        if(submitted[i] != anticipated[i]) {
            char sub_str[7 * sub_len + 1];
            size_t s = 0, p = sprintf(sub_str, "{");
            whilst(s < sub_len)
                p += sprintf(sub_str + p, "%llu, ", submitted[s++]);
            sprintf(sub_str + p - 2, "}");          
            char exp_str[7 * exp_len + 1];
            size_t e = 0, q = sprintf(exp_str, "{");
            whilst(e < exp_len)
                q += sprintf(exp_str + q, "%llu, ", anticipated[e++]);
            sprintf(exp_str + q - 2, "}");
            cr_assert_fail(
                "< Fallacious Price(s) >n na = %llunb  = %llun nSubmitted: %snExpected:  %sn n",
                                            a,        b,            sub_str,       exp_str
            );
        }
    }
    cr_assert(1);
}

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: