Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NEWBIE

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
NEWBIE IN C++ I NEED HELP WITH CODING DIVISIBILITY BY CODE SHOULD PRINT OUT "PRIME" IF NOT DIVISIBLE AND IF IT IS DIVISIBLE NEED TO PRINT OUT NUMBER AND COMMAS AFTER EACH NUMBER .TY IN ADVANCE

OUTOUT

2 PRIME
3 PRIME
4 2
5 PRIME
.
.
.
100 2,4,10,20,25,50
 
write a function to factor a number and return a list of factors MAYBE a CArray, CList??? If all that was returned was 1 and the number it is prime. Also realize there are many mathamatical ways to determine if a number is divisible by 2 (easy) , 3 (all the numbers in the number added together is a number divisible by 3 etc...


Matt
 
let n be that no.

int flag=0;
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
cout << i << &quot;,&quot;;
}
}
if(flag==0)
{
cout << &quot;prime&quot;;
}

this should work nearly the way u want.

luv
Karthik.

 
this recursive function will work faster:
at last you'll get a prime number list in array y.
#include <math.h>
void prime(int *y, int &n)
{
if (y[n]%2 == 0) {
y[n+1] = y[n] / 2;
y[n] = 2;
n++;
prime(y,n);
return;
}else{
int i = 3;
int M = sqrt((double)y[n]);
while (i <= M +1){
if (y[n] %i == 0) {
y[n+1] = y[n] / i;
y[n] = i;
n++;
prime(y,n);
return;
}
i +=2;
}
}
}
void main(){
int y[50];
y[0] = 100;
int n = 0;
prime(y,n);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top