numbers

First N digits of a long number in constant-time?

我的未来我决定 提交于 2019-12-05 02:41:00
问题 In a Project Euler problem I need to deal with numbers that can have hundreds of digits. And I need to perform some calculation on the first 9 digits. My question is: what is the fastest possible way to determine the first N digits of a 100-digit integer? Last N digits are easy with modulo/remainder. For the first digits I can apply modulo 100 times to get digit by digit, or I can convert the number to String and truncate, but they all are linear time. Is there a better way? 回答1: You can

Assembler 64b division

浪子不回头ぞ 提交于 2019-12-05 02:40:10
问题 I need some easy way to divide 64b unsigned integers in assembler for x86. My number is saved in two 32b registers EDX:EAX and I need to put result back to EDX:EAX. Factor is in 32b integer. Some code, please? 回答1: If I interpret your question correctly (particularly the part Factor is in 32b integer ), you want to divide a 64-bit dividend by a 32-bit divisor and get a 64-bit quotient. If that interpretation is correct, then it's actually easy to do in 32-bit code. The idea is that you divide

React.js controlled text cursor focus issue

杀马特。学长 韩版系。学妹 提交于 2019-12-05 02:23:52
I have a simple controlled input of type number like below. <input type="number" value={+value} step={1} onChange={this.updateMyChange} /> My value often returns a decimal number like 123.123 . My problem is, when I try edit the value. The cursor loses focus and shifts to the beginning ignoring the whole numbers as soon as the decimal places are cleared. Like below: How do I address this? Immediately after the decimal places are cleared, the cursor jumps to the beginning thereby making it impossible to edit the whole numbers. Any help would be appreciated. Update Below is the remaining code as

Limit Numbers after Decimal on Key Press Event

流过昼夜 提交于 2019-12-05 02:17:56
问题 I am using the following code to take only digits from user and only one decimal point , that is working fine for me on KeyPress Event : if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) { e.Handled = true; } Now I want to Limit the numbers/Digits after the decimal/dot i.e 35.25468, means it take only 6 numbers/digits after the dot/decimal. Update me ! 回答1: private void

Extract number from string javascript

被刻印的时光 ゝ 提交于 2019-12-05 01:46:06
问题 Is anyone know a way to extract numbers from a string in Javascript ? Example: 1 banana + 1 pineapple + 3 oranges My purpose is to have the result in a array or JSON or something else Result : [1,1,3] 回答1: var result= "1 banana + 1 pineapple + 3 oranges"; result.match(/[0-9]+/g) 回答2: Using String.prototype.match() and parseInt(): let s = "1 banana + 1 pineapple + 3 oranges"; let result = s.match(/\d+/g).map(n => parseInt(n)); console.log(result); 回答3: Use this regex / -> start \d+ -> digit /g

How can I use C# to sort values numerically?

纵然是瞬间 提交于 2019-12-05 01:32:59
I have a string that contains numbers separated by periods. When I sort it appears like this since it is a string: (ascii char order) 3.9.5.2.1.1 3.9.5.2.1.10 3.9.5.2.1.11 3.9.5.2.1.12 3.9.5.2.1.2 3.9.5.2.1.3 3.9.5.2.1.4 etc. I want it to sort like this: (in numeric order) 3.9.5.2.1.1 3.9.5.2.1.2 3.9.5.2.1.3 ... 3.9.5.2.1.9 3.9.5.2.1.10 3.9.5.2.1.11 3.9.5.2.1.12 I know that I can: Use the Split function to get the individual numbers Put the values into an object Sort the object I prefer to avoid all of that work if it is duplicating existing functionality. Is a method in the .net framework

C# wrong subtraction? 12.345 - 12 = 0.345000000000001 [closed]

谁说我不能喝 提交于 2019-12-05 01:32:49
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center . Closed 7 years ago . I am beginner in C# and I am working with floating point numbers. I need to do subtraction between these two numbers but it does not work. I know it is caused by floating point number, but how can I fix it please and if you be so good can you explain me

Java: many ways of casting a (long) Object to double

别说谁变了你拦得住时间么 提交于 2019-12-05 01:17:54
I have an Object obj that I know is actually a long . In some Math code I need it as double . Is it safe to directly cast it to double? double x = (double)obj; Or should I rather cast it first to long and then to double. double x = (double)(long)obj; I also found another (less readable) alternative: double x = new Long((long)obj).doubleValue(); What are the dangers/implications of doing either? Solution Summary : obj is a Number and not a long . Java 6 requires explicit casting, e.g.: double x = ((Number)obj).doubleValue() Java 7 has working cast magic: double x = (long)obj For more details on

PHP: Simple regular expressions to match length?

*爱你&永不变心* 提交于 2019-12-05 00:49:58
I'm creating a registration system that needs to check the name/pass etc. with REGEX (and prefer to), what I've got so far is: //Check so numbers aren't first, such as 00foobar preg_match('/^(?!\d)[a-z0-9]+$/iD',$usrname); //Just simple check preg_match('/^[a-zA-Z0-9]+$/',$psword); But I have to do stupid things in IF statements like: if strlen($psword) > 30 || if (strlen($psword) < 4) .... How would I impliment the length checking in my two original regular expression statements? This would make me so happy.. RageZ same but using the \w and \d for word and digits, but you might want also to

support new Indian currency symbol in java

廉价感情. 提交于 2019-12-05 00:46:54
问题 how to format a number with new Indian currency symbol(₹) using java.if there is any workaround let us share . 回答1: Until Java properly supports the new character you can manually specify the currency symbol using its unicode value. The Unicode for the Indian currency symbol is U+20B9 So to insert this character into a java string you specify it as \u20B9 instead of the default DecimalFormat currency value of \u00A4 For example: DecimalFormat formatter = new DecimalFormat("\u20B9 000");