How does this code work

Page 1 of 1

5 Replies - 573 Views - Last Post: 19 December 2008 - 06:33 PM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

How does this code work

Posted 19 December 2008 - 02:35 PM

// Exercise 04.01.03
// This program find the first prime number greater
//  than one billion (1000000000).
// 

#include <iostream>
#include <math.h>
using namespace std;

// Function must be declared before being used.
int prime(int n);

int main() {
	int i;

// Set up an infinite loop; break if user enters 0.
// Otherwise, evaluate n from prime-ness.

	 for (i = 1000000000;; i++) {
						
		 if (! prime(i)) {					 // Call prime(i)
			 cout << i << " is not prime" << endl;
			 break;
		 }
	 }

	return 0;
}

// Prime number function. Test divisors from
//  2 to sqrt of n. Return false if a divisor
//  found; otherwise, return true.

int prime(int n) {
	int i;
	double sqrt_n = sqrt(static_cast<double>(n));

	for (i = 2; i <= sqrt_n; i++) {
		if (n % i == 0)		   // If i divides n evenly,
			return false;		 //  n is not prime.
	}
	return true;   // If no divisor found, n is prime.
}



Just need somebody to explain this in deep...Eps the for loop in main. Thanks! Don't blame on me for asking stupid question. Sorry!

Is This A Good Question/Topic? 0
  • +

Replies To: How does this code work

#2 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 12
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: How does this code work

Posted 19 December 2008 - 02:37 PM

It works the same way the programming of the Matrix itself worked, someone architect'd it and debugged it and took the time to put their thought and understanding of the language together and came up with the code you have there today.

This post has been edited by tody4me: 19 December 2008 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: How does this code work

Posted 19 December 2008 - 02:56 PM

Looks like it finds the first non-prime number greater than 1000000000. The for loop sets no exit condition, requiring a break within the loop to terminate.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How does this code work

Posted 19 December 2008 - 04:35 PM

This line:
for (i = 1000000000;; i++) {


creates a loop that runs from 1000000000 to infinity in steps of one integer.
See how there is no limiting value in the loop so it will not stop of its own accord.
It relies on something in the code block inside the loop to break the loop.
While there is nothing 'wrong' with using a for loop like this it is more normal to use a while() or do-while() loop in such a case.
The rule of thumb is:
- use a for() loop when you know that the loop should run a finite number of times
- use a while() loop when you don't know how many time the loop should run and it might run zero times
- use a do-while() loop when you don't know how many time the loop should run and it must run at least one time
But these are only rules of thumb and can be broken when there is a good reason to do so.

This line:
 if (! prime(i)) { 


Calls the function prime() passing it the current value of i
- prime() returns 'true' or 'false'
- if prime() returns 'true' the if() condition is not satisfied and the code block is not entered, if prime() returns 'false' the if() condition is satisfied and the code block is entered
- if the code block is entered the statement is printed on the screen AND the for() loop ends

So I would expect the code to:
- test 1,000,000,000
- find that 1,000,000,000 is not prime
- end

Given that this is not what the description of the code says it will do it is buggy code and needs to be fixed.

This post has been edited by janotte: 19 December 2008 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 numerical_jerome  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 167
  • Joined: 16-September 07

Re: How does this code work

Posted 19 December 2008 - 04:50 PM

determining a nine digit prime this way would take days,

see thread:

http://www.dreaminco...h...st&p=481766
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How does this code work

Posted 19 December 2008 - 06:33 PM

Looks possibly (or rather sounds) like you got this code from another site, and are asking how it works so when you turn it in as your own you don't have to worry about being able to answer questions about how it work or what it does, but that's just my opinion.

You know, this don't help you in the real world, taking others work and claiming it as your own to pass a class isnt going to help when you take away a programming job from an actual well qualified programmer.

I could be wrong here (but I doubt it) because that's the only reason I can think of for someone to post code then ask how it works. Shouldn't it stand to reason that if you actually wrote the code in question that you would already know how it works and what it does?

Just so you know, Dream.In.Code isn't here to offer that kind of help, we're here to help those who actually put in the work to write their code to do their assignments (and by [b]putting in the work[/i] I'm not referring to using Google until you find what you're looking for), then run into problems and ask for help in solving a particular problem.

I do believe you've received all the explanations you're going to receieve as this topic is now closed :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1