logarithm

What would cause an algorithm to have O(log log n) complexity?

一个人想着一个人 提交于 2019-11-26 19:14:38
This earlier question addresses some of the factors that might cause an algorithm to have O(log n) complexity. What would cause an algorithm to have time complexity O(log log n)? O(log log n) terms can show up in a variety of different places, but there are typically two main routes that will arrive at this runtime. Shrinking by a Square Root As mentioned in the answer to the linked question, a common way for an algorithm to have time complexity O(log n) is for that algorithm to work by repeatedly cut the size of the input down by some constant factor on each iteration. If this is the case,

How do you calculate log base 2 in Java for integers?

荒凉一梦 提交于 2019-11-26 18:44:29
问题 I use the following function to calculate log base 2 for integers: public static int log2(int n){ if(n <= 0) throw new IllegalArgumentException(); return 31 - Integer.numberOfLeadingZeros(n); } Does it have optimal performance? Does someone know ready J2SE API function for that purpose? UPD1 Surprisingly for me, float point arithmetics appears to be faster than integer arithmetics. UPD2 Due to comments I will conduct more detailed investigation. UPD3 My integer arithmetic function is 10 times

What is the best data structure for storing a set of four (or more) values?

南楼画角 提交于 2019-11-26 16:38:11
问题 Say I have the following variables and its corresponding values which represents a record . name = 'abc' age = 23 weight = 60 height = 174 Please note that the value could be of different types ( string , integer , float , reference-to-any-other-object, etc). There will be many records (at least >100,000). Each and every record will be unique when all these four variables (actually its values ) are put together. In other words, there exists no record with all 4 values are the same. I am

Building a logarithm function in C without using float type

╄→尐↘猪︶ㄣ 提交于 2019-11-26 15:33:20
I need to rewrite the log function (base 2 or base 10 doesn't matter which) without using float type, but I need to get the precision of few decimal digits after the decimal point. ( like a float * 100 to get 2 decimals inside integer type eg: if the 1.4352 would be the result, my function should return something like 143 ( int type) and I know that the last 2 numbers are the decimals. I found over the stackoverflow some methods like: How can I compute a base 2 logarithm without using the built-in math functions in C#? but all of them returns int precision ( avoiding the decimals ). I have no

Convert Linear scale to Logarithmic

こ雲淡風輕ζ 提交于 2019-11-26 15:19:40
问题 I have a linear scale that ranges form 0.1 to 10 with increments of change at 0.1: |----------[]----------| 0.1 5.0 10 However, the output really needs to be: |----------[]----------| 0.1 1.0 10 (logarithmic scale) I'm trying to figure out the formula needed to convert the 5 (for example) to 1.0. Consequently, if the dial was shifted halfway between 1.0 and 10 (real value on linear scale being 7.5), what would the resulting logarithmic value be? Been thinking about this for hours, but I have

Logarithmic slider

谁都会走 提交于 2019-11-26 15:07:57
问题 I have a slider with values ranging from 0 to 100. I want to map them to a range from 100 to 10,000,000. I've seen some functions scattered around the net but they're all in C++. I need it in Javascript. Any ideas? 回答1: You can use a function like this: function logslider(position) { // position will be between 0 and 100 var minp = 0; var maxp = 100; // The result should be between 100 an 10000000 var minv = Math.log(100); var maxv = Math.log(10000000); // calculate adjustment factor var

Logarithm for BigInteger

时光总嘲笑我的痴心妄想 提交于 2019-11-26 14:27:00
问题 I have a BigInteger number, for example beyond 2 64 . Now i want to calculate the logarithm of that BigInteger number, but the method BigInteger.log() does not exist. How do I calculate the (natural) logarithm of my large BigInteger value? 回答1: If you want to support arbitrarily big integers, it's not safe to just do Math.log(bigInteger.doubleValue()); because this would fail if the argument exceeds the double range (about 2^1024 or 10^308, i.e. more than 300 decimal digits ). Here's my own

Logarithm of a BigDecimal

a 夏天 提交于 2019-11-26 12:22:56
How can I calculate the logarithm of a BigDecimal? Does anyone know of any algorithms I can use? My googling so far has come up with the (useless) idea of just converting to a double and using Math.log. I will provide the precision of the answer required. edit: any base will do. If it's easier in base x, I'll do that. Peter Java Number Cruncher: The Java Programmer's Guide to Numerical Computing provides a solution using Newton's Method . Source code from the book is available here . The following has been taken from chapter 12.5 Big Decmial Functions (p330 & p331): /** * Compute the natural

How to do an integer log2() in C++?

末鹿安然 提交于 2019-11-26 12:18:29
In the C++ standard libraries I found only a floating point log method. Now I use log to find the level of an index in a binary tree ( floor(2log(index)) ). Code (C++): int targetlevel = int(log(index)/log(2)); I am afraid that for some of the edge elements (the elements with value 2^n) log will return n-1.999999999999 instead of n.0. Is this fear correct? How can I modify my statement so that it always will return a correct answer? You can use this method instead: int targetlevel = 0; while (index >>= 1) ++targetlevel; Note: this will modify index. If you need it unchanged, create another

Histogram with Logarithmic Scale and custom breaks

三世轮回 提交于 2019-11-26 09:00:18
问题 I\'m trying to generate a histogram in R with a logarithmic scale for y. Currently I do: hist(mydata$V3, breaks=c(0,1,2,3,4,5,25)) This gives me a histogram, but the density between 0 to 1 is so great (about a million values difference) that you can barely make out any of the other bars. Then I\'ve tried doing: mydata_hist <- hist(mydata$V3, breaks=c(0,1,2,3,4,5,25), plot=FALSE) plot(rpd_hist$counts, log=\"xy\", pch=20, col=\"blue\") It gives me sorta what I want, but the bottom shows me the