TCD Posted January 14, 2013 Report Share Posted January 14, 2013 Am si eu o problema de facultate de anul I. Cine ma poate ajuta, va rog?PrimeSe consider? un num?r natural nenul n.Cerin??Scrie?i o func?ie care genereaz? toate posibilit??ile de scriere a lui n ca sum? de numere prime.Exempluintrare |: 8ie?ire : 2+2+2+2;2+3+3; 3+5 Quote Link to comment Share on other sites More sharing options...
fulminator Posted January 14, 2013 Report Share Posted January 14, 2013 Eu am o idee! Cauta pe google prime numbers calculators si vezi algoritmul din spatele scriptului. Li - - nk Quote Link to comment Share on other sites More sharing options...
TCD Posted January 14, 2013 Author Report Share Posted January 14, 2013 Mie mi-ar trebui codul in C pt ca nu ma pricep prea bine Quote Link to comment Share on other sites More sharing options...
spance2life Posted January 21, 2013 Report Share Posted January 21, 2013 Backtracking + functie care verificata daca un nr este prim care o folosesti in BT cum ar fii :int prim(int n){ for(int i=2;i<=sqrt(n);i++) if(!(n%i)) return 0; return 1;} Quote Link to comment Share on other sites More sharing options...
bcman Posted January 21, 2013 Report Share Posted January 21, 2013 Cum a zis Spance, dar mai bine ai folosi Ciurul lui Eratostene, care iti returneaza un vector cu primele k numere prime. Quote Link to comment Share on other sites More sharing options...
skate4 Posted February 13, 2013 Report Share Posted February 13, 2013 #include <stdio.h>#include <conio.h> int n,x[100],v[100],m=0; int prim(int n){ int i; if(n==0 || n==1) return 0; else if(n==2 || n==3) return 1; else if(n%2==0) return 0; for(i=3;i*i<=n;i+=2) if(n%i==0) return 0; return 1; } void afis(int n){ int i; for(i=1;i<=n;i++) printf("%d ",x[i]); putchar('\n'); } void back(int k,int sp){ int i; for(i=1;i<=m;i++){ x[k]=v[i]; sp=sp+x[k]; if(sp<=n && x[k]>x[k-1]) if(sp==n) afis(k); else back(k+1,sp); sp=sp-x[k]; } } main(){ int i; puts("\nDati n: "); scanf("%d",&n); for(i=2;i<=n;i++) if(prim(i)) { m++; v[m]=i; } back(1,0); getch(); }cam asta ar fi .. trebuie sa stapanesti conceptul de backtracking prima data , si abia apoi poti jongla cu asa ceva. Quote Link to comment Share on other sites More sharing options...
cmiN Posted February 16, 2013 Report Share Posted February 16, 2013 Banuiesc ca esti la FII si-ti trebuia pentru tema5, aici gasesti problema. Alte surse mai poti gasi in box.com/cmiN -> FII/L1/AP, oricum s-a terminat semestrul, da' cine stie, pe viitor poate o sa gasesti chestii interesante. Quote Link to comment Share on other sites More sharing options...