Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Digit-increasing number test

A number is called digit-increasing if it is equal n + nn + nnn + ... for some digit n between 1 and 9. For example 24 is digit-increasing because it equals 2 + 22 (here n = 2).

Actually, a friend of mine asked me this question and i am stuck thinking about it but couldn't find the exact solution so far. Can anyone help ? I needed the function that returns true if it is digit-increasing else false.

like image 707
nebula Avatar asked Oct 30 '12 07:10

nebula


People also ask

How to find the digit increasing number between 1-9?

Here is a python code.The basic logic here is that a digit increasing number if divided by a specific number between 1-9 gives a digit increasing number made of only ones.All the digit increasing numbers of 1 follow a specific pattern ie 12345678... Show activity on this post. I have done in this way.

Are the digits in increasing order?

If flag is true, digits are not in increasing order, else they are in increasing order. Enter a number : 123456 Digits are in increasing order. Enter a number : 1234586 Digits are not in increasing order.

Are the digits in 54321 in increasing order?

But for the number 54321, they are not in increasing order. The user will first enter one number and our program will start scanning its digits from right to left. It will compare the rightmost element to the element left to it.

What is digit-increasing number?

A number is called digit-increasing if it is equal n + nn + nnn + ... for some digit n between 1 and 9. For example 24 is digit-increasing because it equals 2 + 22 (here n = 2). Actually, a friend of mine asked me this question and i am stuck thinking about it but couldn't find the exact solution so far. Can anyone help ?


1 Answers

I have done in this way. Check out once.

int sum = 0, count =0;
bool flag = false;

public bool isDigitIncreasing(int input_number)
{
int  n= get_number_of_digit(input_number); // Gets number of digits
int sum = 0;
    for(int i=0;i<n;i++)
    {
        sum = sum*10+1;
        count = count + sum;
    }

    for(int i=1; i<=9;i++)
    {
        if((input_number)==count*i)
        {
            flag = true;
            break;
        }
        else
        flag = false;
    }
    return flag;

}

    public int get_number_of_digit(int num)
    {

        int size = 0;
        do
        {
            num = num/10;
            size++;
        }while(num>0);
        return size;
    }
like image 114
Sujan Avatar answered Oct 24 '22 20:10

Sujan