You have a positive integer try to find out how many digits it has
You have a positive integer try to find out how many digits it has
Count number of digits in a number in Python
Count the number of digits in a number using python :
Using python, count the number of digits in a number. In this tutorial, we will learn how to count the total number of digits in a number using python. The program will get the input from the user and print out the result.
We will show you two different ways to calculate total digits in a number.
Solution 1 (using a while loop) :
The idea behind this solution is to keep deleting the rightmost digit of the number one by one until the number becomes zero.
You can also download this program from here.
Solution 2 ( Using inbuilt methods ) :
Instead of using a while loop, we can directly convert the integer to a string and get the length of the string. In python, getting the length of a string is easy by using the ’len()’ method. For converting a number to a string, we can use the ’str()’ method.
‘abs’ is used to get the absolute value of a number. i.e. to handle the case even if the number is negative.
For example, if our input value is ’-123‘, abs() will convert it to ’123‘. str() will convert it to a string and len() will return the length of the string or 3.
You can also download this program from here.
You have a positive integer try to find out how many digits it has
we will pick new questions that match your level based on your Timer History
every week, we’ll send you an estimated GMAT score based on your performance
we will pick new questions that match your level based on your Timer History
Not interested in getting valuable practice questions and articles delivered to your email? No problem, unsubscribe here.
Hello Guest!
It appears that you are browsing the GMAT Club forum unregistered!
Signing up is free, quick, and confidential.
Join 700,000+ members and get the full benefits of GMAT Club
Registration gives you:
Tests
Take 11 tests and quizzes from GMAT Club and leading GMAT prep companies such as Manhattan Prep. All are free for GMAT Club members.
Applicant Stats
View detailed applicant stats such as GPA, GMAT score, work experience, location, application status, and more
Books/Downloads
Download thousands of study notes, question collections, GMAT Club’s Grammar and Math books. All are free!
and many more benefits!
GMAT Club Timer Informer
Hi GMATClubber!
We’ll give you
an estimate of
your score
We’ll provide
personalized question
recommendations
Your score will improve
and your results
will be more realistic
My Notes
Max: 2000 Characters
Request Expert Reply
Events & Promotions
How to crack the ISB Interview
Free EA Prep Hour
MBA Admission: Round 1 vs Round 2 | Which is better? (Live Webinar)
Verb Tenses: Understand the timeline! (Free Webinar)
Learn the Meaning-based approach to ace GMAT SC with 90+ % Accuracy
Planning To Take The GMAT In 2022? Here’s A Micro Course That Will Definitely Help.
How to crack the ISB Interview
Free EA Prep Hour
MBA Admission: Round 1 vs Round 2 | Which is better? (Live Webinar)
Verb Tenses: Understand the timeline! (Free Webinar)
Learn the Meaning-based approach to ace GMAT SC with 90+ % Accuracy
Planning To Take The GMAT In 2022? Here’s A Micro Course That Will Definitely Help.
54% (02:15) correct
46% (02:12) wrong
based on 2546 sessions
Hide Show timer Statistics
If m is a positive integer, then m^3 has how many digits?
(1) m has 3 digits.
(2) m^2 has 5 digits.
Most Helpful Expert Reply
If m is a positive integer, then m^3 has how many digits?
Pick some easy numbers.
(1)+(2) The same example worked for both statements so even taken together statements are not sufficient.
Answer: E.
_________________
Most Helpful Community Reply
If m is a positive integer, then m^3 has how many digits?
1. m has 3 digits
2. m^2 has 5 digits
How would you do this quickly? Is there a rule that I am unaware of? I could do it, but I had to pick a few numbers. Thanks.
Back to original qtn:
If m is a positive integer, then \(m^3\) has how many digits?
stmnt1: \(m\) has 3 digits
==> \(10^2 \(10^6 \(m^3\) can have minimum of 7 (i.e 6+1) and max of 11 digits (i.e. 12-1)
hence NOT suff.
stmnt2: m^2 has 5 digits
==> \(10^4 \(10^2 \(10^6 \(m^3\) can have minimum of 7 (i.e 6+1) and max of 8 digits (i.e. 9-1)
hence NOT suff.
stmnt1&2 together: We can conclude that \(m^3\) can have minimum of 6 and max of 8 digits(i.e. 12-1) ==> m can have 7 or 8 digits
hence NOT suff.
General Discussion
If m is a positive integer, then m^3 has how many digits?
1. m has 3 digits
2. m^2 has 5 digits
How would you do this quickly? Is there a rule that I am unaware of? I could do it, but I had to pick a few numbers. Thanks.
If m is a positive integer, then \(m^3\) has how many digits?
stmnt1: \(m\) has 3 digits
==> \(10^2 \(10^6 \(m^3\) can have minimum of 6 and max of 11 digits (i.e. 12-1)
hence NOT suff.
stmnt2: m^2 has 5 digits
==> \(10^4 \(10^2 \(10^6 \(m^3\) can have minimum of 6 and max of 8 digits (i.e. 9-1)
hence NOT suff.
m^3 can have only 7 or 8 digits, not 6. If m=100=10^2 then m^3=10^6 and it has 6 trailing zeros but 7 digits.
_________________
If m is a positive integer, then m^3 has how many digits?
1. m has 3 digits
2. m^2 has 5 digits
How would you do this quickly? Is there a rule that I am unaware of? I could do it, but I had to pick a few numbers. Thanks.
1. m has 3 digits
When I look at such statements, I invariably think of the extremities. (as muralimba did above)
Smallest m = 100 which implies m^3 = 10^6 giving 7 digits.
Largest m = 999 but it is not easy to find its cube so I take a number close to it i.e. 1000 and find its cube which is 10^9 i.e. smallest 10 digit number. Hence 999^3 will have 9 digits.
Since we can have 7, 8 or 9 digits, this statement is not sufficient.
2. m^2 has 5 digits
Now try to forget what you read above. Just focus on this statement.
Smallest m^2 = 10000 which implies m = 100
Largest m^2 is less than 99999 which gives m as something above 300 but less than 400.
Now, if m is 100, m^3 = 10^6 giving 7 digits.
If m is 300, m^3 = 27000000 giving 8 digits.
Since we have 7 or 8 digits for m, this statement is not sufficient.
e.g. statement 1 tells us that m has 3 digits. Statement 2 tells us that m is between 100 and 300 something, so statement 2 tells us that m has 3 digits (what statement 1 told us) and something extra (that its value lies between 100 and 300 something). Statement 2 is more informative and is not sufficient alone. Since statement 1 doesn’t add any new information to statement 2, no way will they both together be sufficient.
Hence answer (E).
_________________
For Individual GMAT Study Modules, check Study Modules >
For Private Tutoring, check Private Tutoring >
How do I find out how many digits in an integer?
Is there a short way to determine how many digits are in an integer? I’m new to C++, and writing a program that’s supposed to a number in one column, and its square in the next. I’m using setw to manipulate the width of the coulms.
Here is those code:
This works fine as is, but when I change MAX to 1000, the output gets screwed. I know it’s because neither column is wide enough to hold the output, so they get jammed together. I want to make the program robust, so that the output would scale to accomodate shrinking and growing MAX.
I have an idea that if I can do something like this it would work:
- 6 Contributors 12 Replies 446 Views 2 Years Discussion Span Latest Post 11 Years Ago Latest Post by Student level
>>Is there a short way to determine how many digits are in an integer
In a loop keep dividing it by 10 until the result is 0
> I think you misunderstood the other poster.
> I don’t think that word means what you think it means.
I amaze at the arrogance and rudeness some people possess. Do you really believe yourself superior to AD’s intellect, and mine?
I don’t really care much what you think. …
All 12 Replies
>>Is there a short way to determine how many digits are in an integer
In a loop keep dividing it by 10 until the result is 0
I thought of writing a function to do that, but I was sort of looking for an inline solution to see maybe if it was possible. I feel like I’m missing what the book is trying to get me to learn here. Anyway for the sake of moving on I’lll roll with this.
Question’s still open for anyone who might know an inline solution.
Inline solution below
Question’s still open for anyone who might know an inline solution.
Also, you want to set the width to the width of the maximum number you’ll have to print in each column. Setting it to the width of i or square is probably not what you want, because the widths of your columns will change for different values.
I think you misunderstood the other poster.
He didn’t misunderstand.
There are two ways to look at «inline»:
1. the OP wants to avoid using function call syntax
2. AD’s code actually avoids calling a function
Either way the best solution takes multiple lines of code, as posted.
I don’t think that word means what you think it means.
There are two ways to look at «inline»:
The OP was using the way AD wasn’t.
I think you misunderstood the other poster.
> I think you misunderstood the other poster.
> I don’t think that word means what you think it means.
I amaze at the arrogance and rudeness some people possess. Do you really believe yourself superior to AD’s intellect, and mine?
I don’t really care much what you think. I know what the word inline means. Look it up yourself.
> The OP was using the way AD wasn’t.
Thank you Captain Obvious.
> I think you misunderstood the other poster.
> I don’t think that word means what you think it means.
I amaze at the arrogance and rudeness some people possess.
I don’t understand why you would be touched off by straightforward responses. Also, haven’t you ever seen The Princess Bride?
Do you really believe yourself superior to AD’s intellect, and mine?
Relative to AD’s, I don’t know.
Ok ok. Just to clarify I wanted to see if I could’ve done it all within the same function, maybe inline was the wrong word to use. I was hoping someone would point out some sort of manipulator or function or something that I might have missed and was supposed to learn.
Who knows, maybe the book meant for me to use a function, come to think of it.
If anyone has the book Accelerated C++ it’s one of the exercises at the end of Chapter Four (or five if you don’t start counting at zero). Anyway thanks AD, although Sarehu is right in that the columns still get screwed because I would have to know the length of the widest one before hand. I guess I have to calculate them all first and then store them and print them back.
Thanks for the help guys. I’m marking this as solved, since the solution I had in mind looks quite unreasonable in hindsight.
EDIT: instead of calculating before hand and storing, if I base the length calculations off MAX it should work just fine 🙂
Welcome to Markdown editor
Hello there! This is your first time usingMarkdown editor Welcome page displayed. If you want to learn how to use the Markdown editor, you can read this article carefully to learn about the basic syntax of Markdown.
New change
We have carried out some function expansion and grammar support to the Markdown editor. In addition to the standard Markdown editor functions, we have added the following new functions to help you use it to blog:
Function shortcut
Withdraw: Ctrl/Command + Z
Redo: Ctrl/Command + Y
Bold: Ctrl/Command + B
Italic: Ctrl/Command + I
Title: Ctrl/Command + Shift + H
Unordered list: Ctrl/Command + Shift + U
Ordered list: Ctrl/Command + Shift + O
Check list: Ctrl/Command + Shift + C
Insert code: Ctrl/Command + Shift + K
Insert link: Ctrl/Command + Shift + L
Insert picture: Ctrl/Command + Shift + G
Find: Ctrl/Command + F
Replace: Ctrl/Command + G
Reasonable creation of headings is helpful for the creation of the catalog
Enter directly once # And press space After that, a level 1 heading will be generated.
Enter 2 times # And press space Later, level 2 headings will be generated.
and so on, we support level 6 headings. Help to use TOC After the grammar, a perfect directory is generated.
How to change the style of text
Emphasized text Emphasized text
Bold text Bold text
2 10 The result of the operation is 1024.
Insert link and image
Picture with dimensions:
Centered and dimensioned pictures:
Of course, in order to make it more convenient for users, we have added an image drag and drop function.
How to insert a beautiful piece of code
Generate a list that suits you
Create a table
A simple table is created like this:
project | Value |
---|---|
computer | $1600 |
Cell phone | $12 |
catheter | $1 |
Setting content is centered, left, right
first row | the second list | Third column |
---|---|---|
The first column of text is centered | The second column of text is right | The third column of text is left |
SmartyPants
SmartyPants converts ASCII punctuation characters into «smart» printed punctuation HTML entities. E.g:
Create a custom list
How to create a footnote
A text with footnotes. 2
Comments are also essential
Markdown converts the text toHTML 。
KaTeX mathematical formula
You can render LaTeX mathematical expressions usingKaTeX:
You can find more information aboutLaTeX Mathematical expressionhere.
Find out how many binary digits a particular integer has [duplicate]
What is the fastest possible way to find out how many binary digits a particular integer has when it is converted from decimal to binary in C/C++?
So 47 has 6 digits represented in binary.
9 Answers 9
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
For a quick fun way of doing this without needing to call math functions, check this one out:
As a bonus, this should cook down to a memory load and 2 registers in use, for extra whiz-bang.
If you’re looking for the «fastest» way in terms of performance, you will need to resort to platform-specific methods.
Some architectures actually have an instruction that does that.
On x86, you have the bsr instruction.
In MSVC, it is accessible as:
The fastest solution that’s presented at my favorite collection of bit twiddling hacks is Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup. It requires 13 instructions to find the highest set bit in a number.
The traditional way
You can get more fancy with optimization.
EDIT 2 Here’s the fastest code I could think of without the use of Bit Scan Reverse opcode. You could use a bigger (256 entry) LUT and remove the last IF statement. In my testing this was faster than the repeated OR-SHIFT then LUT method described in another post.
Or if your in x86 or x86-64 bit architecture you can use the BSR (Bit Scan Reverse) opcode. You can find the c++ intrinsic for it http://msdn.microsoft.com/en-us/library/fbxyd7zd%28v=vs.80%29.aspx
EDIT Why the log2 answers are not optimal. While mathematically correct, complex floating point operations (sine, cosine, tan, log) are the slowest performing operations on modern computers. This is compounded by having to convert integer to a float and having to ceiling/floor it as well.
Источники информации:
- http://gmatclub.com/forum/if-m-is-a-positive-integer-then-m-3-has-how-many-digits-167735.html
- http://www.daniweb.com/programming/software-development/threads/102221/how-do-i-find-out-how-many-digits-in-an-integer
- http://programmersought.com/article/15307347877/
- http://stackoverflow.com/questions/8991024/find-out-how-many-binary-digits-a-particular-integer-has