d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Prime Number Algorithm > Basic Program
Add Reply New Topic New Poll
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 2 2013 02:59pm
Hi, I need to write an algorithm that checks if a given number is a prime number or not in c++. It should be written in a function and if its a prime number it should return true. Otherwise the function should return false..

Thanks.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 2 2013 03:01pm
what are the bounds for the assignment? are you expected to take in numbers up to a certain digit count?

the most dumb way is to check for divisors starting from the input/2, then looping down to 2. there are other, better heuristics you can employ to shorten the computation, but there is no catchall function for testing primeness of any arbitrary number.

http://en.wikipedia.org/wiki/Primality_test

This post was edited by irimi on Oct 2 2013 03:03pm
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Member
Posts: 495
Joined: Apr 26 2013
Gold: 0.79
Oct 2 2013 04:58pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll