numbers

Most efficient code for the first 10000 prime numbers?

I want to print the first 10000 prime numbers. Can anyone give me the most efficient code for this? Clarifications: It does not matter if your code is inefficient for n >10000. The size of the code does not matter. You cannot just hard code the values in any manner. ...

SQL, Auxiliary table of numbers

For certain types of sql queries, an auxiliary table of numbers can be very useful. It may be created as a table with as many rows as you need for a particular task or as a user defined function that returns the number of rows required in each query. What is the optimal way to create such a function? ...

How do I tell if a variable has a numeric value in Perl?

Is there a simple way in Perl that will allow me to determine if a given variable is numeric? Something along the lines of: if (is_number($x)) { ... } would be ideal. A technique that won't throw warnings when the -w switch is being used is certainly preferred. ...

Validate numbers in JavaScript - IsNumeric()

What's the cleanest, most effective way to validate decimal numbers in JavaScript? Bonus points for: Clarity. Solution should be clean and simple. Cross-platform. Test cases: 1. IsNumeric('-1') => true 2. IsNumeric('-1.5') => true 3. IsNumeric('0') => true 4. IsNumeric('0.42') => true 5. Isnumeric('.42') => true 6. IsNUmeric(...

Searching for phone numbers in mysql

I have a table which is full of arbitrarily formatted phone numbers, like this 027 123 5644 021 393-5593 (07) 123 456 042123456 I need to search for a phone number in a similarly arbitrary format ( e.g. 07123456 should find the entry (07) 123 456 The way I'd do this in a normal programming language is to strip all the non-digit chara...

Octal number literals: when? why? ever?

I have never used octal numbers in my code nor come across any code that used it (hexadecimal and bit twiddling notwithstanding). I started programming in C/C++ about 1994 so maybe I'm too young for this? Does older code use octal? C includes support for these by prepending a 0, but where is the code that uses these base 8 number litera...

How to read values from numbers written as words?

As we all know numbers can be written either in numerics, or called by their names. While there are a lot of examples to be found that convert 123 into one hundred twenty three, I could not find good examples of how to convert it the other way around. Some of the caveats: cardinal/nominal or ordinal: "one" and "first" common spelling ...

Determine if a string is an integer or a float in ANSI C

Using only ANSI C, what is the best way to, with fair certainty, determine if a C style string is either a integer or a real number (i.e float/double)? ...

Single most effective practice to prevent arithmetic overflow and underflow

What is the single most effective practice to prevent arithmetic overflow and underflow? Some examples that come to mind are: testing based on valid input ranges validation using formal methods use of invariants detection at runtime using language features or libraries (this does not prevent it) ...

How to convert floats to human-readable fractions?

Let's say we have 0.33, we need to output "1/3". If we have "0.4", we need to output "2/5". The idea is to make it human-readable to make the user understand "x parts out of y" as a better way of understanding data. I know that percentages is a good substitute but I was wondering if there was a simple way to do this? ...

How can I convert a number to its multiple form in Perl?

Hello everybody, Do you know an easy and straight-forward method/sub/module which allows me to convert a number (say 1234567.89) to an easily readable form - something like 1.23M? Right now I can do this by making several comparisons, but I'm not happy with my method: if($bytes > 1000000000){ $bytes = ( sprintf( "%0.2f", $bytes/10...

String to Int in java - Likely bad data, need to avoid exceptions.

Seeing as Java doesn't have nullable types, nor does it have a TryParse(), how do you handle input validation without throwing an exceptions? The usual way: String userdata = /*value from gui*/ int val; try { val = Integer.parseInt(userdata); } catch (NumberFormatException nfe) { // bad data - set to sentinel val = Integer.MIN...

What is the best way to validate a credit card in PHP?

Given a credit card number and no additional information, what is the best way in PHP to determine whether or not it is a valid number? Right now I need something that will work with American Express, Discover, MasterCard, and Visa, but it might be helpful if it will also work with other types. ...

Unique random numbers in O(1)?

The problem is this: I'd like to generate unique random numbers between 0 and 1000 that never repeat (I.E. 6 doesn't come out twice), but that doesn't resort to something like an O(N) search of previous values to do it. Is this possible? ...

How to get the Nth digit of an integer with bit-wise operations?

Example. 123456, and we want the third from the right ('4') out. The idea in practise is to access each digit seperately (ie. 6 5 4 3 2 1). C/C++/C# preferred. ...

Parse Phone Number into component parts

I need a well tested Regular Expression (.net style preferred), or some other simple bit of code that will parse a USA/CA phone number into component parts, so: 3035551234122 1-303-555-1234x122 (303)555-1234-122 1 (303) 555 -1234-122 etc... all parse into: AreaCode: 303 Exchange: 555 Suffix: 1234 Extension: 122 ...

Is there a difference between is_int() and ctype_digit()?

Is one more preferred, or performs better over the other? ...

How to count unique records and get number of these uniques in table using SQL?

Hi, Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this is an example of large internet mall, where there are shops which sell to one customer, so customer can choose more products from each shop and buy it in one basket. However, I am not sure if there is an...

random number with ratio 1:2

I have to generate two random sets of matrices Each containing 3 digit numbers ranging from 2 - 10 like that matrix 1: 994,878,129,121 matrix 2: 272,794,378,212 the numbers in both matrices have to be greater then 100 and less then 999 BUT the mean for both matrices has to be in the ratio of 1:2 or 2:3 what ever c...

How do I round a number in javascript?

Hey all. While working on a project, I came across a js-script created by a former employee that basically creates a report in the form of Name : Value Name2 : Value2 etc... Problem for me though, is that the values can sometimes be floats (with different precision), integers, or even in the form "2.20011E+17" What I outputted thoug...