Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.
3K
Solutions
4K
Contributors
An algorithm is a self-contained step-by-step set of operations to be performed. Algorithms exist that perform calculation, data processing, and automated reasoning. Starting from an initial state and initial input (perhaps empty), the instructions describe a computation that, when executed, proceeds through a finite number of well-defined successive states, eventually producing "output" and terminating at a final ending state. The transition from one state to the next is not necessarily deterministic; some algorithms, known as randomized algorithms, incorporate random input.
Sign up to Post
Load Recent Content
Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.
Expert Comment
by:fidspliceExpert Comment
by:Ray Paseurpublic int fibonacci(int n) {
int nth = 0;
int nMinus2 = 0; // (n - 2)th fibonacci
int nMinus1 = 1; // (n - 1)th fibonacci
if(n == 0) return 0;
if(n == 1) return 1;
for(int i = 2; i <= n; ++i) {
nth = nMinus1 + nMinus2; // nth fibonacci
nMinus2 = nMinus1;
nMinus1 = nth;
}
return nth; // nth fibonacci
}
// This method takes one parameter P
// to check if P is prime
public boolean isPrime(int P) {
for(int i = 2; i < P; ++i) {
if(P % i == 0) return false;
}
return true;
}
Approach 2: Whenever there is a divisor greater than square-root(P), there must be another divisor less than or equal to square-root(P) but NOT greater than square-root(P). For example: Divisors of 20 are: 20 = 1, 2, 4, 5, 10 and 20. For each of these pairs there MUST be least one divisor that is less than or equal to square-root(P).public boolean isPrime(int P) {
// i * i <= P is similar to i <= sqrt(p)
for(int i = 2; i * i <= P; ++i) {
if(P % i == 0) return false;
}
return true;
}
Approach 3: The only prime number that is even is 2. Any other even number cannot be prime because all even numbers are divisible by 2. So, instead of dividing by all the numbers in range 0 to (P - 1) we’ll divide only by odd numbers up to sqrt(P).
…
Expert Comment
by:J. Andrew Smithfor (int i=2, boolean *pp=p+2; i <= sqrt_n /* see #1 above */; i++, pp++)
if (*pp)
for (Boolean *ppp=pp+i, int j=i; (j+=i) <= n; )
*(ppp += i) = false;
Of course the lower loop has two additions per iteration, so there are no savings there.
Administrative Comment
by:Nadia Sobnompublic int gcd(int a, int b) {
for(int div = minimum(a, b); div >= 1; --div) {
if(a % div == 0 && b % div == 0) {
return div;
}
}
}
Let's consider two other numbers 540 and 600 and find their gcd. Author Comment
by:Justin SmithAuthor Comment
by:Justin SmithExpert Comment
by:aikimarkExpert Comment
by:ozoprotected void Page_Load(object sender, EventArgs e)
{
// Read in the width and height
int maxHeight, maxWidth;
string h = Request.QueryString["h"];
string w = Request.QueryString["w"];
if (h == null || h == "")
maxHeight = Int32.MaxValue;
else
maxHeight = Int32.Parse(h);
if (w == null || w == "")
maxWidth= Int32.MaxValue;
else
maxWidth = Int32.Parse(w);
string imageUrl = Request.QueryString["img"];
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl));
// Do we need to create a thumbnail?
Response.ContentType = "image/gif";
if (fullSizeImg.Height > maxHeight || fullSizeImg.Width > maxWidth)
{
//resize stuff
int newWidth, newHeight;
if (fullSizeImg.Height >= maxHeight && fullSizeImg.Width < maxWidth)
{
newHeight =
…
3K
Solutions
4K
Contributors
An algorithm is a self-contained step-by-step set of operations to be performed. Algorithms exist that perform calculation, data processing, and automated reasoning. Starting from an initial state and initial input (perhaps empty), the instructions describe a computation that, when executed, proceeds through a finite number of well-defined successive states, eventually producing "output" and terminating at a final ending state. The transition from one state to the next is not necessarily deterministic; some algorithms, known as randomized algorithms, incorporate random input.