ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi everybody
this time i am stuck on the following problem:
let's suppose i have an array[2,3,5,7] and a certain number n.
my goal is to create a new array in the following way.
the integer n divide array[0] till some condition is true; if false n start to divide array[1] till the same condition is true........ if false n divide array[3] (last component) till the same condition is true.
all the time the condition is not verify, before the cycle goes to the next step i need to count the number of division.
in the end in the new array I'd like to see integers which one represent the number of division while the condition impose is true.
I'm going also to post the entire code; the output of the code are: a list of prime numbers minor or equal to n, the difference between consecutive prime numbers,the total number of prime numbers in the range (2;n), the list of prime factor and the list of the exponents of the prime factor to rebuild n.
here the code
and sorry no time for good indentation
Code:
#include <stdio.h>
#define N 5000
int insert(int *v) {
int i, n;
printf("controlliamo se n e' >= 2 e in tal caso procediamo\n");
printf("2 e' l unico numero primo pari, per controllare se n e' primo basta vedere se nella colonna dei numeri primi generati\n");
printf("l ultimo numero coincide con n, se coincide il numero e' primo altrimenti non lo e'\n");
printf("controlliamo se n e' pari o dispari\n");
printf("troviamo i numeri primi <= di n con il Crivello di Eratostene\n");
printf("attenzione se inserisci un numero con il punto o con la virgola il codice\n");
printf("va in loop e per fermarlo occorre premere ctrl+c e ricompilarlo per ripartire\n\n\n\n");
while (n<2) {
printf("il numero deve essere >=2, per favore inseriscilo qui:");
scanf("%d", &n);
}
if (n%2 == 0)
printf("il numero e' positivo e pari\n");
else
printf("il numero e' positivo e dispari\n");
printf("\n");
/*generiamo un vettore di zeri*/
for(i=2; i<=n; i++)
v[i]=0;
return n;
}
void eratostene(int *v, int n) {
int i,j;
for(i=2; i<=n; i++)
if(v[i]==0)
for(j=2*i; j<=n; j+=i)
v[j]=1; //let's set v[j]=1, to mark all the multiple number ; that's the sieve
}
void print(int *v, int n) {
int i,k;
k=0;
for(i=2; i<=n; i++)
if(v[i]==0) //if the number is prime
k=k+1; //count the number of prime numbers
printf("\nnumeri primi compresi tra 2 e %d sono %d:\n\n",n,k);
}
int primo (int *v, int *q,int n) {//memorizzo i primi nel vettore q[i] ma contiene anche gli zeri
int i,j;
for(i=2; i<=n; i++)
if(v[i]==0)
q[i]=i; //vettore che contiene i numeri primi e gli zeri come distanze
}
int terzo (int *q,int *z,int n) { //creo un nuovo vettore di primi senza gli zeri
int i,j,M,flag;
M=0; //dimensione nuovo vettore
for(i=2; i<=n; i++)
if(q[i]!=0) {
flag=0;
for(j=0;j<=M ;j++)
if(z[j]==q[i])
flag=1;
if(flag==0)
z[M]=q[i];
M=M+1;
}//chisura ciclo if
printf("numeri primi memorizzati in un vettore\n"); //ho eliminato gli zeri da q nel vettore z
for(i=0; i<M; i++)
printf("%d\t",z[i]);
}
int quarto (int *z,int *w) { //trovo il vettore delle differenze tra due numeri primi successivi
int i,j,M;
printf("\n\n");
printf("vettore differenze tra due primi successivi\n");
for(i=0; i<M; i++) {
w[i]=z[i+1]-z[i]; }
// printf("\nvettore delle distanze ossia intervalli aperti tra due primi successivi\n");
for(i=0; i<M-1; i++) {
printf("%d\t",w[i]); }
}
int quinto (int *z,int *f,int n) { //funzione che memorizza i fattori di n inclusi gli zeri in un vettore f
int i,j,M,flag;
printf("\n");
printf("stampiamo i fattori primi di %d\n",n);
for(i=0; i<M; i++) {
if(n%z[i]==0)
f[i]=z[i];
}
}
int sesto (int *z,int *f,int *g,int n) {
int i,j,L,M,flag,k;
L=0; //dimensione nuovo vettore
for(i=0; i<n; i++)
if(f[i]!=0) {
flag=0;
for(j=0;j<=L ;j++)
if(g[j]==f[i])
flag=1;
if(flag==0)
g[L]=f[i];
L=L+1; //L e' la dimensione del vettore che contiene i fattori di n senza gli zeri
}//chisura ciclo if
// printf("\nfattori primi memorizzati in un vettore\n"); //ho eliminato gli zeri da f
for(i=0; i<L; i++) {
printf("%d\n",g[i]);}
}
int ottavo (int *g,int n) { //funzione che stampa gli esponenti dei fattori primi
int i,k,tmp;
printf("esponenti dei fattori primi\n");
for(i=0;i<n;i++){
k=0;
tmp=g[i];
while(n%tmp==0){
n=n/tmp;
k=k+1; }
printf ("%d\n",k);
}
}
int main() {
int a[N],b[N],c[N],d[N],e[N],x[N],y[N],n; //a=v; b=q, c=z; d=w; e=f; g=x; h=y
n=insert(a);
eratostene(a,n);
print(a,n);
primo(a,b,n);
terzo(b,c,n);
quarto(c,d);
quinto(c,e,n);
sesto(c,e,x,n);
ottavo(x,n);
return 0; }
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.