0% found this document useful (0 votes)
17 views5 pages

Cel Mai Mare Divisor Comun: #Include Using Namespace STD

The document provides code snippets in C++ for solving various mathematical problems: 1) Finding the greatest common divisor of two numbers using Euclid's algorithm. 2) Checking if a number is a palindrome. 3) Finding all palindromic numbers between two limits. 4) Finding "superpalindromes" - numbers that are palindromic and whose square is also palindromic. 5) Summing the digits of a number. 6) Reversing a number. 7) Calculating the sum of the first n natural numbers. 8) Factoring a number into prime factors.

Uploaded by

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

Cel Mai Mare Divisor Comun: #Include Using Namespace STD

The document provides code snippets in C++ for solving various mathematical problems: 1) Finding the greatest common divisor of two numbers using Euclid's algorithm. 2) Checking if a number is a palindrome. 3) Finding all palindromic numbers between two limits. 4) Finding "superpalindromes" - numbers that are palindromic and whose square is also palindromic. 5) Summing the digits of a number. 6) Reversing a number. 7) Calculating the sum of the first n natural numbers. 8) Factoring a number into prime factors.

Uploaded by

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

Tipareste toate numerele naturale <= 10 000 care se pot descompune in doua

moduri diferite ca suma de cuburi

n=i3 + j3

i<

j<
i [1, max]
j [i, max]

max = | |
n = i13 + j13 = i23 + j23

Sa se scrie in pseudocod:

Cel mai mare divisor comun

#include <iostream>
using namespace std;

int main()
{
int a,b;
cin>>a>>b;
int m=a, n=b;
while(a!=b)
{
if(a>b) {a=a-b;}
else{b=b-a;}
}
cout<<"cel mai mare divizor comun "<<a;
cout<<"cel mai mic multiplu comun "<<(m*n)/a;
}

#include <iostream>
using namespace std;

int cmmdc(int m, int n)


{
while(m!=n)
{
if(m>n) {m=m-n;}
else{n=n-m;}
}
return m;
}

int main()
{
int dc,a,b;
cin>>a>>b;
dc=cmmdc(a,b);
cout<<"cel mai mare divizor comun "<<dc<<endl;
cout<<"cel mai mic multiplu comun "<<(a*b)/dc;
}

Palindrom
Citeste m<n, afla toate numerele palindrom intre m si n

#include <iostream>
using namespace std;

int palindrom(int n)
{
int orig=n, inv=0;
while(n)
{
inv=inv*10+i%10;
n=n/10;
}
return (orig==inv);
}

int main()
{
int m,n,i;
cin>>m>>n
for(i=m;i<n;i++)
{
if(palindrom(i)) {cout<<i<<endl;}
}
return 0;
}

superpalindrom

n=12321= palindrom
daca n2= palindrom=>n=superpalindrom

#include <iostream>
using namespace std;

int palindrom(int n)
{
int orig=n, inv=0;
while(n)
{
inv=inv*10+i%10;
n=n/10;
}
return (orig==inv);
}

int main()
{
long m,n,i;
cin>>m>>n
for(i=m;i<n;i++)
{
if(palindrom(i))
{
if(palindrome(i*i)) { cout<<i<<" "<<i*i<<endl;}
}
}

return 0;
}

Citeste un numar natural si scrie suma cifrelor lui

int main()
{
int nr,sum=0;
cin>>nr;
while(nr)
{ sum=sum+nr%10;
nr=nr/10;
}
return 0;
}

Inverseaza numarul:

#include <iostream>
using namespace std;

int main()
{
int nr,inv=0;
cin>>nr;
while(nr)
{ inv=inv*10+nr%10;
nr=nr/10;
}
cout<<inv;
return 0;
}

Do while

Calculeaza suma primelor n numere

#include <iostream>
using namespace std;
int main()
{
int nr,sum=0,i=1;
cin>>nr;
do
{ sum=sum+i;
i=i+1;
} while(i<nr);
cout<<sum;
return 0;
}

Descompune in factori primi un numar natural

#include <iostream>
using namespace std;

int main()
{
int nr,fm,i=2;
cin>>nr;
do
{ fm=0;
while(nr%i==0)
{ fm++;
nr/=i;
}
if(fm) cout<<i<<"la puterea "<<fm<<endl;
i++;
}while(nr!=1);
return 0;
}

You might also like