Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how many numbers between 1 to 10 billion contains 14

i tried this code but it takes so long and I can not get the result

    public long getCounter([FromBody]object req)
    {
        JObject param = Utility.GetRequestParameter(req);
        long input = long.Parse(param["input"].ToString());
        long counter = 0;
        for (long i = 14; i <= input; i++)
        {
            string s = i.ToString();
            if (s.Contains("14"))
            {
                counter += 1;
            }
        }
        return counter;
    }

please help

like image 735
Vivi Avatar asked Jun 22 '18 07:06

Vivi


People also ask

How many numbers are there between 1 and 1000 divisible by 5 or 7?

∴ 16 numbers will be there.

How many numbers are there between 1 and 100?

There are 98 whole numbers between 1 and 100.

How many numbers between 1 and 100 contain the digit 8?

Also, the digit 8 occurs 10 times from the number 80 to the number 89 in the tens place. Hence, the total number of times the number 8 occurs from 1 to 100 can be calculated as shown below. Therefore, 8 occur 20 times in the number from 1 to 100.

What is the number form of 1 14 billion?

The number form of 1.14 billion is 1140000000. It can also be abbreviated as 1.14B. 1.14 billion is 1,140,000,000

How many numbers bigger than a trillion?

Numbers Bigger Than a Trillion Name Number of Zeros Groups of (3) Zeros Ten 1 (10) Hundred 2 (100) Thousand 3 1 (1,000) Ten thousand 4 (10,000) 22 more rows ...

How many zeros does 10 billion have?

Then you may see that the 10 billion in numbers takes more space but if we write that down in scientific notation then it will look like this : How many zeros does 10 billion have? When we count zeros in 10 billion above, we see that there are 10 zeros.

How much is 10 billion in numbers?

10 billion in numbers How much is 10 billion in numbers? The number form of 10 billion is 10000000000. It can also be abbreviated as 10B.


1 Answers

We can examine all non-negative numbers < 10^10. Every such number can be represented with the sequence of 10 digits (with leading zeroes allowed).

How many numbers include 14

Dynamic programming solution. Let's find the number of sequences of a specific length that ends with the specific digit and contains (or not) subsequence 14: enter image description here

F(len, digit, 0) is the number of sequences of length len that ends with digit and do not contain 14, F(len, digit, 1) is the number of such sequences that contain 14. Initially F(0, 0, 0) = 1. The result is the sum of all F(10, digit, 1).

C++ code to play with: https://ideone.com/2aS17v. The answer seems to be 872348501.

How many times the numbers include 14

First, let's place 14 at the end of the sequence:

????????14

Every '?' can be replaced with any digit from 0 to 9. Thus, there are 10^8 numbers in the interval that contains 14 at the end. Then consider ???????14?, ??????14??, ..., 14???????? numbers. There are 9 possible locations of 14 sequence. The answer is 10^8 * 9 = 90000000.


[Added by Matthew Watson]

Here's the C# version of the C++ implementation; it runs in less than 100ms:

using System;

namespace Demo
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            const int M = 10;
            int[,,] f = new int [M + 1, 10, 2];

            f[0, 0, 0] = 1;

            for (int len = 1; len <= M; ++len)
            {
                for (int d = 0; d <= 9; ++d)
                {
                    for (int j = 0; j <= 9; ++j)
                    {
                        f[len,d,0] += f[len - 1,j,0];
                        f[len,d,1] += f[len - 1,j,1];
                    }
                }
                f[len,4,0] -= f[len - 1,1,0];
                f[len,4,1] += f[len - 1,1,0];
            }

            int sum = 0;

            for (int i = 0; i <= 9; ++i)
                sum += f[M,i,1];

            Console.WriteLine(sum); // 872,348,501
        }
    }
}
like image 122
DAle Avatar answered Oct 31 '22 08:10

DAle