Not Yet Answered # Prime numbers

jackabascal Discussion Starter literal sunyifei23 Discussion Starter literal sunyifei23 firstPerson 761 Discussion Starter literal Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

confusion 1:

j = two.

while j is less than or equal to i divided by j

j = j + one

confusion 2:

if j is less than i divided by j

output a fraction in the form i over j.

0

confusion 1:

j = two.

while j is less than or equal to i divided by j

j = j + oneconfusion 2:

if j is less than i divided by j

output a fraction in the form i over j.

thx but I know that already, what does that actually mean inside the program?

0

confusion 1: Loop used to test if i is divisible by any number, the condition j<=(i/j) is used so that numbers wont be checked twice. if no number is divisible, then the loop will not break in between result j>(i/j)

*Edited 6 Years Ago by sunyifei23*: spelling error

0

This is how the author of the book explained it:

"You can stop at the value

of i / j because a number that is larger than i / j cannot be a factor of i"

0

for example 6 = 1*6 or 2*3 or 3*2 or 6*1, and you want to find out if 6 is divisible by any other number, you only have to go through 1,2 and 3 instead of 1-6

0

That code is kind of confusing if you do not know the meaning of i/j.

Take i = 10 for example, and we want to see if 10 is a prime number. To do this, what

your code does is run to all numbers from j = 2 to j < i/j. If it successfuly runs through the whole loop then its a prime, if not then your "confusion2" checks if the loop ended prematurely.

Personally, that code is looks confusing to beginners, and I would suggest something like this to beginners. That is after they have learned about function.

```
#include <iostream>
using namespace std;
//returns true, if the 'lhs' is evenly divisible by 'rhs'
bool isEvenlyDivisible(int lhs, int rhs){
return lhs % rhs == 0;
}
/* A number is a prime if its evenly divisionable by
1 and its self only. Ex, 2 is a prime because 2 divides into 1 evenly
and 2 divideds to 2 evenly. Anything else divided into 2 has a remainder */
//function returns true if 'num' is a prime otherwise returns false
bool isPrime(int num){
//if num < 2 then its by definition not a prime
if(num < 2) return false;
//initial condition, assume num is prime
bool isAPrime = true;
//runs through i = 2 to num, to check if any number evenly divides into 'num'
//notice how checkFacotor is never equal to 1, or num. So if its divisible by any
//number in between, then 'num' is not a prime
for(int checkFactor = 2; checkFactor < num; ++checkFactor){
//is num is evenly divisible by checkFactor, where checkFactor is not 1 or num
if(isEvenlyDivisible(num,checkFactor)){
isAPrime = false; //then num is not a prime
break; // since its not a prime then exit the loop
}
}
return isAPrime;
}
int main()
{
//runs through all numbers in range of [0,100) to check if its a prime
for(int i = 0; i < 100; ++i)
{
if(isPrime(i)){
cout << i << " is a prime\n";
}
}
}
```

0

Can you actually show how this code works through the actual numbers ? It'd be easier to comprehend.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...