numbers

Can coordinates of constructable points be represented exactly?

岁酱吖の 提交于 2019-11-30 07:13:20
I'd like to write a program that lets users draw points, lines, and circles as though with a straightedge and compass. Then I want to be able to answer the question, "are these three points collinear?" To answer correctly, I need to avoid rounding error when calculating the points. Is this possible? How can I represent the points in memory? (I looked into some unusual numeric libraries, but I didn't find anything that claimed to offer both exact arithmetic and exact comparisons that are guaranteed to terminate.) Yes. I highly recommend Introduction to constructions , which is a good basic

How do I validate an Australian Medicare number?

会有一股神秘感。 提交于 2019-11-30 06:58:16
I'm developing an online form in which user-entered Medicare numbers will need to be validated. (My specific problem concerns Australian Medicare numbers, but I'm happy for answers regarding American ones too. This question is about Medicare numbers in general.) So how should I do it? (It would be good to have the answer in Javascript or a regex.) The regex supplied by Jeffrey Kemp (March 11) would help to validate the allowed characters, but the check algorithm below should be enough to validate that the number conforms to Medicare's rules. The Medicare card number comprises: Eight digits; A

Format string to a 3 digit number

白昼怎懂夜的黑 提交于 2019-11-30 06:53:56
问题 Instead of doing this, I want to make use of string.format() to accomplish the same result: if (myString.Length < 3) { myString = "00" + 3; } 回答1: If you're just formatting a number, you can just provide the proper custom numeric format to make it a 3 digit string directly: myString = 3.ToString("000"); Or, alternatively, use the standard D format string: myString = 3.ToString("D3"); 回答2: string.Format("{0:000}", myString); 回答3: It's called Padding: myString.PadLeft(3, '0') 回答4: (Can't

How to Generate Unique Number of 8 digits?

最后都变了- 提交于 2019-11-30 06:53:19
I am using this code to generate a 8 digit unique number. byte[] buffer = Guid.NewGuid().ToByteArray(); return BitConverter.ToUInt32(buffer, 8).ToString(); Does this code really generate a unique number or might it repeat the same number again? Marnix van Valen A GUID is not just a random number; it's composed of segments. Some of the segments will not change at all if the guid is generated on the same computer. By using only 64-bits of the original 128-bits you are breaking the structure of the guid and most likely breaking the uniqueness of the generated number. This question has more info

Iterate through each digit in a number

梦想与她 提交于 2019-11-30 06:52:09
I am trying to create a program that will tell if a number given to it is a " Happy Number " or not. Finding a happy number requires each digit in the number to be squared, and the result of each digit's square to be added together. In Python, you could use something like this: SQUARE[d] for d in str(n) But I can't find how to iterate through each digit in a number in Java. As you can tell, I am new to it, and can't find an answer in the Java docs. You can use a modulo 10 operation to get the rightmost number and then divide the number by 10 to get the next number. long addSquaresOfDigits(int

Algorithm for finding smallest number with given number of factors

十年热恋 提交于 2019-11-30 06:47:28
What's the most efficient algorithm anyone can think of that, given a natural number n , returns the least natural number x with n positive divisors (including 1 and x )? For example, given 4 the algorithm should result in 6 (divisors: 1,2,3,6); i.e. 6 is the smallest number having 4 distinct factors. Similarly, given 6, the algorithm should result in 12 (divisors: 1,2,3,4,6,12); i.e. 12 is the smallest number having 6 distinct factors In terms of real-world performance, I'm looking for a scalable algorithm which can give answers of the order of 10 20 within 2 seconds on a machine which can do

Random prime number

你。 提交于 2019-11-30 06:40:04
How do I quickly generate a random prime number, that is for sure 1024 bit long? Generate 1024 random bits. Use a random source that is strong enough for your intended purpose. Set the highest and lowest bits to 1. This makes sure there are no leading zeros (the prime candidate is big enough) and it is not an even number (definitely not prime). Test for primality . If it's not a prime, go back to 1. Alternatively, use a library function that generates primes for you. Use a library function, such as OpenSSL. There's no need to write this yourself. Example: http://ardoino.com/7-maths-openssl

What is the maximum value of a number in Lua?

故事扮演 提交于 2019-11-30 06:37:46
问题 There doesn't seem to be a clear answer to this in the documentation. I'm interested in incrementing a variable time that counts the seconds since the program started. If the maximum value can count far into the future, like 100 years, then I don't care about letting the variable increment forever. Otherwise I'm going to have to think of a good point to reset time back to 0. 回答1: as compiled by default, the Number is a double , on most compilers that's an IEEE 64-bit floating point. that

Count the number of Ks between 0 and N

心不动则不痛 提交于 2019-11-30 06:22:49
问题 Problem: I have seen questions like: count the number of 0s between 0 and N? count the number of 1s between 0 and N? count the number of 2s between 0 and N? ... ... These kinds of questions are very similar of asking to find the total number that Ks (i.e. K=0,1,2,...,9) are shown in number range [0, N] . Example: Input: K=2, N=35 Output: 14 Detail: list of 2 s between [0,35] : 2, 12, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32 , note that 22 will be counted as twice (as 22 contains two 2 s)

How do I check if input is a number in Python?

南笙酒味 提交于 2019-11-30 06:02:54
问题 I have a Python script which converts a decimal number into a binary one and this obviously uses their input. I would like to have the script validate that the input is a number and not anything else which will stop the script. I have tried an if/else statement but I don't really know how to go about it. I have tried if decimal.isint(): and if decimal.isalpha(): but they just throw up errors when I enter a string. print("Welcome to the Decimal to Binary converter!") while True: print("Type a