Search Results

Search found 5960 results on 239 pages for 'numbers'.

Page 3/239 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • C code to round numbers

    - by webgenius
    Is there any way to round numbers in C? I do not want to use ceil and floor. Is there any other alternative? I came across this code snippet when I Googled for the answer: (int)(num < 0 ? (num - 0.5) : (num + 0.5)) The above line always prints the value as 4 even when float num =4.9. Please suggest a solution.

    Read the article

  • Algorithm to generate N random numbers between A and B which sum up to X

    - by Shaamaan
    This problem seemed like something which should be solvable with but a few lines of code. Unfortunately, once I actually started to write the thing, I've realized it's not as simple as it sounds. What I need is a set of X random numbers, each of which is between A and B and they all add up to X. The exact variables for the problem I'm facing seem to be even simpler: I need 5 numbers, between -1 and 1 (note: these are decimal numbers), which add up to 1. My initial "few lines of code, should be easy" approach was to randomize 4 numbers between -1 and 1 (which is simple enough), and then make the last one 1-(sum of previous numbers). This quickly proved wrong, as the last number could just as well be larger than 1 or smaller than -1. What would be the best way to approach this problem? PS. Just for reference: I'm using C#, but I don't think it matters. I'm actually having trouble creating a good enough solution for the problem in my head.

    Read the article

  • 3 threads printing numbers of different range

    - by user875036
    This question was asked in an Electronic Arts interview: There are 3 threads. The first thread prints the numbers 1 to 10. The second thread prints the numbers 11 to 20. The third thread prints the numbers from from 21 to 30. Now, all three threads are running. The numbers are printed in an irregular order like 1, 11, 2, 21, 12 etc. If I want numbers to be printed in sorted order like 1, 2, 3, 4..., what should I do with these threads?

    Read the article

  • Prolog: find all numbers of unique digits that can be formed from a list of digits

    - by animo
    The best thing I could come up with so far is this function: numberFromList([X], X) :- digit(X), !. numberFromList(List, N) :- member(X, List), delete(List, X, LX), numberFromList(LX, NX), N is NX * 10 + X. where digit/1 is a function verifying if an atom is a decimal digit. The numberFromList(List, N) finds all the numbers that can be formed with all digits from List. E.g. [2, 3] -> 23, 32. but I want to get this result: [2, 3] -> 2, 3, 23, 32 I spent a lot of hours thinking about this and I suspect you might use something like append(L, _, List) at some point to get lists of lesser length. I would appreciate any contribution.

    Read the article

  • Smarty multiple random numbers list

    - by Heinrich
    Hey Stackoverflow-Folks, is there any smart way to post random numbers (e.g. 1-4) in a list by using the smarty tpl-engine? standart list sorted 1-5: <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> Here's my solution (PHP): <ul> {foreach from=randomNumbers} <li>{smarty.randomNumbers}</li> {/foreach} </ul> modified list sorted 1-5 (random): <ul> <li>3</li> <li>2</li> <li>5</li> <li>1</li> <li>4</li> </ul> I've really tested nearly everything, but I do only need a smart & small solution for this :-) Kind Regards, Heinrich

    Read the article

  • Prime Numbers Code Help

    - by andrew
    Hello Everybody, I am suppose to "write a Java program that reads a positive integer n from standard input, then prints out the first n prime number." It's divided into 3 parts. 1st: This function will return true or false according to whether m is prime or composite. The array argument P will contain a sufficient number of primes to do the testing. Specifically, at the time isPrime() is called, array P must contain (at least) all primes p in the range 2 p m . For instance, to test m = 53 for primality, one must do successive trial divisions by 2, 3, 5, and 7. We go no further since 11 53 . Thus a precondition for the function call isPrime(53, P) is that P[0] = 2 , P[1] = 3 , P[2] = 5, and P[3] = 7 . The return value in this case would be true since all these divisions fail. Similarly to test m =143 , one must do trial divisions by 2, 3, 5, 7, and 11 (since 13 143 ). The precondition for the function call isPrime(143, P) is therefore P[0] = 2 , P[1] = 3 , P[2] = 5, P[3] = 7 , and P[4] =11. The return value in this case would be false since 11 divides 143. Function isPrime() should contain a loop that steps through array P, doing trial divisions. This loop should terminate when 2 either a trial division succeeds, in which case false is returned, or until the next prime in P is greater than m , in which case true is returned. Then there is the "main function" • Check that the user supplied exactly one command line argument which can be interpreted as a positive integer n. If the command line argument is not a single positive integer, your program will print a usage message as specified in the examples below, then exit. • Allocate array Primes[] of length n and initialize Primes[0] = 2 . • Enter a loop which will discover subsequent primes and store them as Primes[1] , Primes[2], Primes[3] , ……, Primes[n -1] . This loop should contain an inner loop which walks through successive integers and tests them for primality by calling function isPrime() with appropriate arguments. • Print the contents of array Primes[] to stdout, 10 to a line separated by single spaces. In other words Primes[0] through Primes[9] will go on line 1, Primes[10] though Primes[19] will go on line 2, and so on. Note that if n is not a multiple of 10, then the last line of output will contain fewer than 10 primes. The last function is called "usage" which I am not sure how to execute this! Your program will include a function called Usage() having signature static void Usage() that prints this message to stderr, then exits. Thus your program will contain three functions in all: main(), isPrime(), and Usage(). Each should be preceded by a comment block giving it’s name, a short description of it’s operation, and any necessary preconditions (such as those for isPrime().) And hear is my code, but I am having a bit of a problem and could you guys help me fix it? If I enter the number "5" it gives me the prime numbers which are "6,7,8,9" which doesn't make much sense. import java.util.; import java.io.; import java.lang.*; public class PrimeNumber { static boolean isPrime(int m, int[] P){ int squarert = Math.round( (float)Math.sqrt(m) ); int i = 2; boolean ans=false; while ((i<=squarert) & (ans==false)) { int c= P[i]; if (m%c==0) ans= true; else ans= false; i++; } /* if(ans ==true) ans=false; else ans=true; return ans; } ///****main public static void main(String[] args ) { Scanner in= new Scanner(System.in); int input= in.nextInt(); int i, j; int squarert; boolean ans = false; int userNum; int remander = 0; System.out.println("input: " + input); int[] prime = new int[input]; prime[0]= 2; for(i=1; i ans = isPrime(j,prime); j++;} prime[i] = j; } //prnt prime System.out.println("The first " + input + " prime number(s) are: "); for(int r=0; r }//end of main } Thanks for the help

    Read the article

  • Problems in "Save as PDF" plugin with Arabic numbers

    - by Mohamed Mohsen
    I use the "Save as PDF" plugin with Word 2007 to generate a PDF document from a DOCX document. It works great except that the Arabic numbers in the Word file have been converted to English numbers in the PDF document. Kindly find two links containing two screen shots explaining the problem. The first image is the generated PDF file with the English numbers highlighted. The second image is the original word file with the Arabic numbers highlighted. Update: Thanks very much Isaac, ChrisF and Wil. I changed the Numeral at word to Context and confirmed that all the numbers are Arabic at the Word file. I still have the problem as the PDF file still have English numbers. (Note: The Arabic numbers called Hindi numbers). I also tried changing the font to Tahoma with no hope.

    Read the article

  • Pseudo random numbers in php

    - by pg
    I have a function that outputs items in a different order depending on a random number. for example 1/2 of the time Popeye's and its will be #1 on the list and Taco Bell and its logo will be #2 and half the time the it will be the other way around. The problem is that when a user reloads or comes back to the page, the order is re-randomized. $Range here is the number of items in the db, so it's using a random number between 1 and the $range. $random = mt_rand(1,$range); for ($i = 0 ; $i < count($variants); $i++) { $random -= $variants[$i]['weight']; if ($random <= 0) { $chosenoffers[$tag] = $variants[$i]; break; } } I went to the beginning of the session and set this: if (!isset($_SESSION['rannum'])){ $_SESSION['rannum']=rand(1,100); } With the idea that I could replace the mt_rand in the function with some sort of pseudo random generator that used the same 1-100 random number as a seed throughout the session. That way i won't have to rewrite all the code that was already written. Am I barking up the wrong tree or is this a good idea?

    Read the article

  • C: Random Number Generation - What (If Anything) Is Wrong With This

    - by raoulcousins
    For a simple simulation in C, I need to generate exponential random variables. I remember reading somewhere (but I can't find it now, and I don't remember why) that using the rand() function to generate random integers in a fixed range would generate non-uniformly distributed integers. Because of this, I'm wondering if this code might have a similar problem: //generate u ~ U[0,1] u = ( (double)rand() / ((double)(RAND_MAX)); //inverse of exponential CDF to get exponential random variable expon = -log(1-u) * mean; Thank you!

    Read the article

  • VB.net Need Text Box to Only Accept Numbers

    - by Rico Jackson
    I'm fairly new to VB.net (self taught) and was just wondering if someone out there could help me out with some code. I'm not trying to do anything to invovled, just have a textbox that takes numeric value from 1 to 10. I don't want it to take a string or any number above 10. If some types a word or character an error message will appear, telling them to enter a valid number. This is what I have, obviously it's not great as I am having problems. Thanks again to anyone who can help. If TxtBox.Text > 10 Then MessageBox.Show("Please Enter a Number from 1 to 10") TxtBox.Focus() ElseIf TxtBox.Text < 10 Then MessageBox.Show("Thank You, your rating was " & TxtBox.Text) Total = Total + 1 ElseIf IsNumeric(TxtBox.Text) Then MessageBox.Show("Thank you, your rating was " & ValueTxtBox.Text) End If ValueTxtBox.Clear() ValueTxtBox.Focus()

    Read the article

  • Help with float numbers in Java

    - by Alvin
    Hi, Could anyone please me why the output of the following programme is not " different different"? public static void main(String[] args) { float f1=3.2f; float f2=6.5f; if(f1==3.2) System.out.println("same"); else System.out.println("different"); if(f2==6.5) System.out.println("same"); else System.out.println("different"); } o/p :different same

    Read the article

  • Eclipse Subversive revision numbers on multiple project commit

    - by CannyDuck
    If I have 2 projects in Eclipse that refers to the same repository location. repository location: svn://server project-module1 - svn://server/trunk/project-module1 project-module2 - svn://server/trunk/project-module2 So if I sync the project change with Subversive and have a change in module1 and module2 that refers to the same context I select all files and perform one commit, but if I look into my project revisions after that I see that 2 revisions were created. One for module1 and one for module2 with the same comment. How can I change the behave that only one revision number is created?

    Read the article

  • php numbers: assert( 1.0 < 2.0 )

    - by xtofl
    How can this <?php assert( 1.0 < 2.0 ); ?> result in Warning: assert() [function.assert]: Assertion failed in C:\Program Files (x86)\wamp\www\test.php on line 2 Edit: depending on the file I put this code in, 1.0 < 2.0 evaluates to false or true.

    Read the article

  • Get 100 highest numbers from an infinite list

    - by Sachin Shanbhag
    One of my friend was asked this interview question - "There is a constant flow of numbers coming in from some infinite list of numbers out of which you need to maintain a datastructure as to return the top 100 highest numbers at any given point of time. Assume all the numbers are whole numbers only." This is simple, you need to keep a sorted list in descending order and keep a track on lowest number in that list. If new number obtained is greater than that lowest number then you have to remove that lowest number and insert the new number in sorted list as required. Then question was extended - "Can you make sure that the Order for insertion should be O(1)? Is it possible?" As far as I knew, even if you add a new number to list and sort it again using any sort algorithm, it would by best be O(logn) for quicksort (I think). So my friend told it was not possible. But he was not convinced, he asked to maintain any other data structure rather than a list. I thought of balanced Binary tree, but even there you will not get the insertion with order of 1. So the same question I have too now. Wanted to know if there is any such data structure which can do insertion in Order of 1 for above problem or it is not possible at all.

    Read the article

  • Efficient algorithm to find a the set of numbers in a range

    - by user133020
    If I have an array of sorted numbers and every object is one of the numbers or multiplication. For example if the sorted array is [1, 2, 7] then the set is {1, 2, 7, 1*2, 1*7, 2*7, 1*2*7}. As you can see if there's n numbers in the sorted array, the size of the set is 2n-1. My question is how can I find for a given sorted array of n numbers all the objects in the set so that the objects is in a given interval. For example if the sorted array is [1, 2, 3 ... 19, 20] what is the most efficient algorithm to find the objects that are larger than 1000 and less than 2500 (without calculating all the 2n-1 objects)?

    Read the article

  • How to get unique numbers using randomint python?

    - by user2519572
    I am creating a 'Euromillions Lottery generator' just for fun and I keep getting the same numbers printing out. How can I make it so that I get random numbers and never get the same number popping up: from random import randint numbers = randint(1,50) stars = randint(1,11) print "Your lucky numbers are: ", numbers, numbers, numbers, numbers, numbers print "Your lucky stars are: " , stars, stars The output is just: >>> Your lucky numbers are: 41 41 41 41 41 >>> Your lucky stars are: 8 8 >>> Good bye! How can I fix this? Regards

    Read the article

  • How to write simple code using TDD [migrated]

    - by adeel41
    Me and my colleagues do a small TDD-Kata practice everyday for 30 minutes. For reference this is the link for the excercise http://osherove.com/tdd-kata-1/ The objective is to write better code using TDD. This is my code which I've written public class Calculator { public int Add( string numbers ) { const string commaSeparator = ","; int result = 0; if ( !String.IsNullOrEmpty( numbers ) ) result = numbers.Contains( commaSeparator ) ? AddMultipleNumbers( GetNumbers( commaSeparator, numbers ) ) : ConvertToNumber( numbers ); return result; } private int AddMultipleNumbers( IEnumerable getNumbers ) { return getNumbers.Sum(); } private IEnumerable GetNumbers( string separator, string numbers ) { var allNumbers = numbers .Replace( "\n", separator ) .Split( new string[] { separator }, StringSplitOptions.RemoveEmptyEntries ); return allNumbers.Select( ConvertToNumber ); } private int ConvertToNumber( string number ) { return Convert.ToInt32( number ); } } and the tests for this class are [TestFixture] public class CalculatorTests { private int ArrangeAct( string numbers ) { var calculator = new Calculator(); return calculator.Add( numbers ); } [Test] public void Add_WhenEmptyString_Returns0() { Assert.AreEqual( 0, ArrangeAct( String.Empty ) ); } [Test] [Sequential] public void Add_When1Number_ReturnNumber( [Values( "1", "56" )] string number, [Values( 1, 56 )] int expected ) { Assert.AreEqual( expected, ArrangeAct( number ) ); } [Test] public void Add_When2Numbers_AddThem() { Assert.AreEqual( 3, ArrangeAct( "1,2" ) ); } [Test] public void Add_WhenMoreThan2Numbers_AddThemAll() { Assert.AreEqual( 6, ArrangeAct( "1,2,3" ) ); } [Test] public void Add_SeparatorIsNewLine_AddThem() { Assert.AreEqual( 6, ArrangeAct( @"1 2,3" ) ); } } Now I'll paste code which they have written public class StringCalculator { private const char Separator = ','; public int Add( string numbers ) { const int defaultValue = 0; if ( ShouldReturnDefaultValue( numbers ) ) return defaultValue; return ConvertNumbers( numbers ); } private int ConvertNumbers( string numbers ) { var numberParts = GetNumberParts( numbers ); return numberParts.Select( ConvertSingleNumber ).Sum(); } private string[] GetNumberParts( string numbers ) { return numbers.Split( Separator ); } private int ConvertSingleNumber( string numbers ) { return Convert.ToInt32( numbers ); } private bool ShouldReturnDefaultValue( string numbers ) { return String.IsNullOrEmpty( numbers ); } } and the tests [TestFixture] public class StringCalculatorTests { [Test] public void Add_EmptyString_Returns0() { ArrangeActAndAssert( String.Empty, 0 ); } [Test] [TestCase( "1", 1 )] [TestCase( "2", 2 )] public void Add_WithOneNumber_ReturnsThatNumber( string numberText, int expected ) { ArrangeActAndAssert( numberText, expected ); } [Test] [TestCase( "1,2", 3 )] [TestCase( "3,4", 7 )] public void Add_WithTwoNumbers_ReturnsSum( string numbers, int expected ) { ArrangeActAndAssert( numbers, expected ); } [Test] public void Add_WithThreeNumbers_ReturnsSum() { ArrangeActAndAssert( "1,2,3", 6 ); } private void ArrangeActAndAssert( string numbers, int expected ) { var calculator = new StringCalculator(); var result = calculator.Add( numbers ); Assert.AreEqual( expected, result ); } } Now the question is which one is better? My point here is that we do not need so many small methods initially because StringCalculator has no sub classes and secondly the code itself is so simple that we don't need to break it up too much that it gets confusing after having so many small methods. Their point is that code should read like english and also its better if they can break it up earlier than doing refactoring later and third when they will do refactoring it would be much easier to move these methods quite easily into separate classes. My point of view against is that we never made a decision that code is difficult to understand so why we are breaking it up so early. So I need a third person's opinion to understand which option is much better.

    Read the article

  • Fastest Way to generate 1,000,000+ random numbers in python

    - by Sandro
    I am currently writing an app in python that needs to generate large amount of random numbers, FAST. Currently I have a scheme going that uses numpy to generate all of the numbers in a giant batch (about ~500,000 at a time). While this seems to be faster than python's implementation. I still need it to go faster. Any ideas? I'm open to writing it in C and embedding it in the program or doing w/e it takes. Constraints on the random numbers: A Set of numbers 7 numbers that can all have different bounds: eg: [0-X1, 0-X2, 0-X3, 0-X4, 0-X5, 0-X6, 0-X7] Currently I am generating a list of 7 numbers with random values from [0-1) then multiplying by [X1..X7] A Set of 13 numbers that all add up to 1 Currently just generating 13 numbers then dividing by their sum Any ideas? Would pre calculating these numbers and storing them in a file make this faster? Thanks!

    Read the article

  • how to change some of the numbers in word to be arabic numbers without changing a global setting in windows?

    - by Karim
    i have a word document. it have 2 parts one english and one arabic. the problem is that all the numbers are english numbers [0123456789] but i want the arabic part's numbers to be arabic numbers [??????????] how can i do that in word 2007 or 2010? thanks Edit: since i didnt receive any response to the question i created a software that converts english numbers to arabic and then i use it to convert the numbers in the document. but still wondering if there is a more easy way to do it?

    Read the article

  • How can I access the iPhone favorite numbers using Objective C?

    - by Cipri
    Hi! I am writing an application using Objective C and I want to access the iPhone favorites numbers (the ones that can be found in the "Phone" application). I've searched among the properties specific to each contact from the Address Book and I even had a look at the Address Book database, but I couldn't identify which property or field from the database indicates which are the favorite numbers. Since "Phone" is an application installed by Apple on the iPhone I was expecting to find such a property in the database. However, I think that it might be possible that this application stores the favorite numbers independently of the Address Book database. Has anyone encountered this problem before and if yes, could you please clarify this matter for me? Thanks a lot! Cipri

    Read the article

  • Adding items to a generic list (novice)

    - by Crash893
    I'm some what embarrassed to even ask this but I know there is a better way to do this I just don't know how List<int> numbers = new List<int>(22); numbers.Add(3); numbers.Add(4); numbers.Add(9); numbers.Add(14); numbers.Add(15); numbers.Add(19); numbers.Add(28); numbers.Add(37); numbers.Add(47); numbers.Add(50); numbers.Add(54); numbers.Add(56); numbers.Add(59); numbers.Add(61); numbers.Add(70); numbers.Add(73); numbers.Add(78); numbers.Add(81); numbers.Add(92); numbers.Add(95); numbers.Add(97); numbers.Add(99);

    Read the article

  • How to check if numbers are in correct sequence?

    - by Nazariy
    I have a two dimensional array that contain range of numbers that have to be validated using following rules, range should start from 0 and follow in arithmetic progression. For example: $array = array(); $array[] = array(0);//VALID $array[] = array(0,1,2,3,4,5);//VALID $array[] = array("0","1");//VALID $array[] = array(0,1,3,4,5,6);//WRONG $array[] = array(1,2,3,4,5);//WRONG $array[] = array(0,0,1,2,3,4);//WRONG what is most efficient way to do that in php? UPDATE I forgot to add that numbers can be represented as string

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >