The problem
The quantity 89
is the primary integer with multiple digit that fulfills the property partially launched within the title of this problem. What’s the usage of saying “Eureka”? As a result of this sum provides the identical quantity.
In impact: 89 = 8^1 + 9^2
The subsequent quantity in having this property is 135
.
See this property once more: 135 = 1^1 + 3^2 + 5^3
We want a perform to gather these numbers, that will obtain two integers a
, b
that defines the vary [a, b]
(inclusive) and outputs a listing of the sorted numbers within the vary that fulfills the property described above.
Let’s see some instances (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 type within the vary [a, b] the perform ought to output an empty listing.
90, 100 --> []
The answer in C
Possibility 1:
#embrace <stddef.h>
#embrace <math.h>
typedef unsigned lengthy lengthy ull;
ull *sum_dig_pow(ull a, ull b, ull *outcomes, size_t *size) {
int place = 0;
for (int i = a; i <= b; i++) {
int okay = 0;
okay = log10(i) + 1;
if (pow((i % 10), okay) > i) i = i + (10 - (i % 10));
else {
int op = i;
int sum = 0;
for (int m = okay; m > 0; m--) {
sum += pow(op % 10, m);
op /= 10;
}
if (sum == i) {
outcomes[place] = i;
place++;
}
}
}
*size = place;
return outcomes;
}
Possibility 2:
#embrace <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]; }
}
return r;
}
Possibility 3:
#embrace <stdio.h>
#embrace <string.h>
#embrace <math.h>
#embrace <stdlib.h>
typedef unsigned lengthy lengthy ull;
ull *sum_dig_pow (ull a, ull b, ull *outcomes, size_t *size) {
char str [snprintf (NULL, 0, "%llu", b) + 1];
for (*size = 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)
*(outcomes + (*size)++) = a;
}
return outcomes;
}
Check instances to validate our answer
#embrace <criterion/criterion.h>
#embrace <stddef.h>
#embrace <stdio.h>
typedef unsigned lengthy lengthy ull;
ull *sum_dig_pow(ull a, ull b, ull *outcomes, size_t *size);
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 outcomes[exp_len];
for(size_t i=0; i<exp_len; i++) {
outcomes[i] = rand() - rand();
}
size_t sub_len = 0;
ull *submitted = sum_dig_pow(a, b, (ull *)outcomes, &sub_len);
if(sub_len != exp_len) {
cr_assert_fail(
"< Incorrect Output Size >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, "{");
whereas(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, "{");
whereas(e < exp_len)
q += sprintf(exp_str + q, "%llu, ", anticipated[e++]);
sprintf(exp_str + q - 2, "}");
cr_assert_fail(
"< Incorrect Worth(s) >n na = %llunb = %llun nSubmitted: %snExpected: %sn n",
a, b, sub_str, exp_str
);
}
}
cr_assert(1);
}