how to calculate amount of zeroes? - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html) +--- Forum: QB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-11.html) +--- Thread: how to calculate amount of zeroes? (/thread-2993.html) Pages:
1
2
|
how to calculate amount of zeroes? - Agamemnus - 01-16-2004 I was wondering whether anyone know a quick way to figure out if a number (say n&) is 1, 10, 100, 1000, 10000, 100000, or 1000000, without using loops or long ifs. And without resorting to string methods? Sort of like figuring out if a number is 50 or 10: a% = 50 PRINT a% MOD 2 any ideas? how to calculate amount of zeroes? - whitetiger0990 - 01-16-2004 is it trailing zeros? i.e. 103000 would return 3? how to calculate amount of zeroes? - oracle - 01-16-2004 LOG (base 10) numzeroes% = log(10) 100000& I think... how to calculate amount of zeroes? - SCM - 01-16-2004 You can use LOG(n&) / Ln10, where Ln10 = LOG(10). If n& is a power of ten it will give you the number of zeros. how to calculate amount of zeroes? - whitetiger0990 - 01-16-2004 while were talking about LOG... what exactly is it? o.O how to calculate amount of zeroes? - SCM - 01-16-2004 A log is the power of a base that equals the number. Since 1000 = 10^3, the log base 10 of 1000 is 3. It also works with decimals. 10 = 2^3.3219...., so the log base 2 of 10 is 3.3219.... In QB LOG finds the log of a special base, e = 2.71828... In math classes this log will be called ln (the natural log). To solve Aga's problem, I wanted to find the power of 10, which tells me the number of zeros. The power of 10 is the log base 10. To find that I divided the LOG of the number by the LOG of 10. Log_Base_10 = LOG(Number) / LOG(10) If I wanted the log base 2, I would have divided the LOG of the number by the LOG of 2. Log_Base_2 = LOG(Number) / LOG(2) how to calculate amount of zeroes? - Neo - 01-16-2004 I used a similar algorithm to determine the size of a Big Integer (BigInt library). For numbers, you could do like: Code: n& = 10000 But since my BigIntegers were strings, I was able to do: Code: n$ = "10000" So if you want the INT(LOG), you can also do this with numbers: Code: n& = 10000 I don't know which variant is faster, the LOG(n) / LOG(10) variant or the LEN(STR$(n)) - 2 variant, which is used for the rounded logarithm. how to calculate amount of zeroes? - Lachie Dazdarian - 01-16-2004 Input a If a=10 or a=20 or a=30 or a=40 or a=50 or a=60 or a=70 or a=80 or a=90 or a=100 or a=110 or.... Never mind... how to calculate amount of zeroes? - Neo - 01-16-2004 Are you going to do the same as rel? j/k :lol: how to calculate amount of zeroes? - Agamemnus - 01-16-2004 Thanks. I just wasted 256 bytes and used a certain array though. Faster I think. I am wasteful. |