0% found this document useful (0 votes)
37 views1 page

Algoritmi - C++ New PDF

The document contains code snippets for various number theory problems: 1) Processing the digits of a number and composing a number from its digits. 2) Finding the natural divisors of a number and checking if a number is prime. 3) Calculating the greatest common divisor (GCD) of two numbers in two different ways. 4) Generating the proper divisors of a number and generating the prime divisors of a number. 5) Testing if a number is prime in a given interval and generating the prime divisors of a number.

Uploaded by

gciobanu_1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views1 page

Algoritmi - C++ New PDF

The document contains code snippets for various number theory problems: 1) Processing the digits of a number and composing a number from its digits. 2) Finding the natural divisors of a number and checking if a number is prime. 3) Calculating the greatest common divisor (GCD) of two numbers in two different ways. 4) Generating the proper divisors of a number and generating the prime divisors of a number. 5) Testing if a number is prime in a given interval and generating the prime divisors of a number.

Uploaded by

gciobanu_1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

prelucrarea cifrelor unui numar: compunerea unui numar din cifrele sale:

#include<iostream.h> #include<iostream.h>
int main() int main()
{ int n,c; { int c,nr;
cin>>n; nr=0;
while ( n!=0) while( c>=0 && c<=9 )
{ c=n%10; { nr=nr*10+c;
cout<<c<<" "; cin>>c;
n=n/10; }
} cout<<nr;
} }
divizori naturali verific daca numarul n este prim
#include <iostream.h> #include <iostream.h>
#include <math.h> #include <math.h>
int main() int main()
{ int i,n; { int i,n,prim=1; d=2;prim=1;
cin>>n; cin>>n; while(d<=sqrt(n) && prim)
cout<<"1"<<" "; for (i=2;i<=sqrt(n)&&prim;i++ if (n%d==0) prim=0;
for (i=2; i<=sqrt(n); i++) if (n%i==0) prim=0; else d++;
if (n%i==0) if (prim==1) if(prim)
cout<<i<<" "<<n/i<<" "; cout<<n<<" este prim"; cout<<n<<"numar prim ";
cout<<n<<" "; else cout<<n<<"nu-i prim"; else cout<<n<<"nu-i prim";
} }
calcularea c.m.m.d.c: v1: calcularea c.m.m.d.c: v2:
#include<iostream.h> #include<iostream.h>
int main() int main()
{ int a,b,r; { int a,b;
cin>>a>>b; cin>>a>>b;
while(b!=0) while( a!=b)
{ r=a % b; if (a>b)
a=b; a=a-b;
b=r; else b=b-a;
} cout<<"cmmdc="<<a;
cout<<"cmmdc="<<a; } }
generarea divizorilor proprii ai unui numar : v1 generarea divizorilor proprii ai unui numar : v2
#include<iostream.h> #include<iostream.h>
int main() #include <math.h>
{ int n,i ; int main()
cin>> n; { int n,i ;
cout<< 1; cin>> n;
for(i=2; i<=n/2;i++) cout<<1<<" ";
if (n%i==0) for(i=2;i<=sqrt(n);i++)
cout<<" "<<i; if(n%i==0)
cout<<" "<< n; cout<<i<<" "<< n/i<<" ";
} cout<<" "<<n; }
testarea unui numar prim din intervalul (a, b ): generarea divizorilor primi ai unui numar:
#include<iostream.h> #include<iostream.h>
#include <math.h> #include <math.h>
int main() int main()
{ int a,b,n,i; { int n, i, k;
cin>>a>>b; cin>>n;
if(a<b) i=2;
{for ( n=a; n<=b;n++) //n prim? while( n!=1)
{ if(n%2==0) { if (n%i==0)
i=n; { k=0;
else while (n%i==0)
{ i=3; { k++;
while (i<=n/2) n=n/i;
if(n%i==0) }
i=n; cout<<i<<"la puterea "<<k<<"*";
else i=i+2; }
} i++;
if (i!=n) cout<<n<<" "; }
} }t.
}
else cout<<"a si b nu formeaza un interval"; }

You might also like