How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the C programming language

Table of Contents

Problem Statement

(This task is taken from a   Project Euler   problem.) (All numbers herein are expressed in base ten.)

27   =   128   and   7   is the first power of   2   whose leading decimal digits are   12. The next power of   2   whose leading decimal digits are   12   is   80, 280   =   1208925819614629174706176.

Define     p(L,n)     to be the nth-smallest value of   j   such that the base ten representation of   2j   begins with the digits of   L .

You are also given that:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the C programming language

The code implements a function to count the number of times a given value l appears in a given integer n. The function is called p and takes two integers as arguments. The first argument, l, is the value to look for, and the second argument, n, is the integer to search in. The function returns the number of times that l appears in n.

The function works by first converting l and n to logarithmic values. It then uses a while loop to repeatedly divide l by 10 and multiply n by 10. This effectively moves the decimal point in l and n to the left by one place each time through the loop.

After the loop has finished, the function has effectively converted l and n to their logarithmic values. The function then uses a second while loop to iterate through the digits of n.

For each digit, the function converts the digit to an integer and multiplies it by the factor computed earlier. The result is the value that is being searched for. If the result is equal to l, then the function increments the counter.

After the second loop has finished, the function returns the value of the counter.

The main function calls the p function five times with different values for l and n. The output of the program is:

p(12, 1) = 1
p(12, 2) = 2
p(123, 45) = 2
p(123, 12345) = 4
p(123, 678910) = 2

Source code in the c programming language

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

int p(int l, int n) {
    int test = 0;
    double logv = log(2.0) / log(10.0);
    int factor = 1;
    int loop = l;
    while (loop > 10) {
        factor *= 10;
        loop /= 10;
    }
    while (n > 0) {
        int val;

        test++;
        val = (int)(factor * pow(10.0, fmod(test * logv, 1)));
        if (val == l) {
            n--;
        }
    }
    return test;
}

void runTest(int l, int n) {
    printf("p(%d, %d) = %d\n", l, n, p(l, n));
}

int main() {
    runTest(12, 1);
    runTest(12, 2);
    runTest(123, 45);
    runTest(123, 12345);
    runTest(123, 678910);

    return 0;
}


  

You may also check:How to resolve the algorithm Cramer's rule step by step in the C programming language
You may also check:How to resolve the algorithm Thue-Morse step by step in the C programming language
You may also check:How to resolve the algorithm Population count step by step in the C programming language
You may also check:How to resolve the algorithm Fractal tree step by step in the C programming language
You may also check:How to resolve the algorithm Strip block comments step by step in the C programming language