Find n ' th number in a number system with only 3 and 4

Source: Internet
Author: User

This is seen in the geeksforgeeks when a problem, very good, the topic is

Given a number system with only 3 and 4. Find the nth number in the number system.

First few numbers in the number system Are:3, 4, 33, 34, 43, 44, 333, 334, 343, 344,

433, 434, 443, 444, 3333, 3334, 3343, 3344, 3433, 3434, 3443, 3444, ...

(Topic link)

To put it simply, it is:

Find the nth number in a sequence of numbers consisting of 3 and 4, this sequence is:

3, 4, 33, 34, 43, 44, 333, 334, 343, 344, 433, 434 、......

For example, to find the 5th number, the output is 43.

This is a Zoho interview, the problem is not very difficult.

Looking closely at this sequence, you can see that each number consists of 3 or 4, and we can group this sequence by the number of digits:

The first group is just one digit: 3,4

The second group is a number with only two digits: 33,34,43,44

The third group is a number with only three digits: 333, 334, 343, 344, 433, 434, 443, 444

.......

A closer look at each group, you can see, each group is a regular, nonsense, there must be regularity.

Replace each group of 3 with 0, 4 with 1 instead, then

The first group is: 0,1

The second group is: 00,01,10,11

The third group is: 000,001,010,011,100,101,110,111

So as long as you know the number of requirements is the first few groups on the line, to turn the required n into

n = 2^i + A

find that I and a,i are bits, that is, group I, A is the first group of a (a can be 0)

Then turn a into binary, for this binary, 0 with 3 instead, 1 on the 4 instead, the solution of the problem.

There are other solutions in Geeksforgeeks.


Find n ' th number in a number system with only 3 and 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.