Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- bool Prost(int broj)
- {
- int counter(0);
- for(int i = 1; i<=broj; i++)
- {
- if(broj % i == 0)
- counter++;
- }
- if(counter == 2)
- return true;
- return false;
- }
- std::vector<int> ProstiBrojevi(int n)
- {
- int counter(0), pocetni(2);
- std::vector<int> rez;
- while(counter < n)
- {
- if(Prost(pocetni))
- {
- rez.push_back(pocetni);
- counter++;
- }
- pocetni++;
- }
- return rez;
- }
- std::vector<int> ProstiBrojeviBlizanci(int n)
- {
- int counter(0), pocetni(2);
- std::vector<int> rez;
- while(counter < n)
- {
- if(Prost(pocetni) && Prost(pocetni+2))
- {
- rez.push_back(pocetni);
- rez.push_back(pocetni+2);
- counter++;
- pocetni+=2;
- }
- pocetni++;
- }
- return rez;
- }
- std::vector<int> ProstiFaktori(int n)
- {
- std::vector<int> rez;
- if(n == 1)
- rez.push_back(n);
- else
- {
- int a(n), b;
- int c(a%2);
- while(c == 0)
- {
- b = a/2;
- rez.push_back(2);
- a = b;
- c = a%2;
- }
- for(int i(3); i<=a; i=i+2)
- {
- int c(a%i);
- while(c == 0)
- {
- b = a/i;
- rez.push_back(i);
- a = b;
- c = a%i;
- }
- }
- }
- return rez;
- }
- std::vector<int> Djelioci(int n)
- {
- std::vector<int> rez;
- for(int i(1); i<=n; i++)
- {
- if(n % i == 0)
- rez.push_back(i);
- }
- return rez;
- }
- int main()
- {
- std::vector<int> v1 = ProstiBrojevi(5);
- for(unsigned int i(0); i < v1.size(); i++)
- std::cout << v1[i] << " ";
- std::cout << std::endl;
- v1 = ProstiBrojeviBlizanci(3);
- for(unsigned int i(0); i < v1.size(); i++)
- std::cout << v1[i] << " ";
- std::cout << std::endl;
- v1 = ProstiFaktori(178);
- for(unsigned int i(0); i < v1.size(); i++)
- std::cout << v1[i] << " ";
- std::cout << std::endl;
- v1 = Djelioci(81);
- for(unsigned int i(0); i < v1.size(); i++)
- std::cout << v1[i] << " ";
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement