Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created December 17, 2024 13:08
Show Gist options
  • Save juanfal/acdeee19bf04f2000466d4046e0bcdd4 to your computer and use it in GitHub Desktop.
Save juanfal/acdeee19bf04f2000466d4046e0bcdd4 to your computer and use it in GitHub Desktop.
non-repeated primes
// 1.nonRepPrime.cpp
// juanfc 2024-12-16
//
// 1 3 4 6 2 3 5 11 0 -> 2 5 11
// 1 3 2 3 2 0 -> 0
// 1 1 1 0 -> 0
#include <iostream>
#include <array>
using namespace std;
const int MAX_NPRIMES = 10;
struct TNum {
int n = 0;
bool rep = false;
};
typedef array<TNum,MAX_NPRIMES> TVec;
int main()
{
void add(TVec& a, int n);
TVec a;
int max = 0;
int n;
while (cin >> n and n != 0)
add(a, n);
bool thereWasOne = false; // not necessary
for (int i = 0; a[i].n > 0; ++i)
if (not a[i].rep) {
cout << a[i].n << " ";
thereWasOne = true;
}
if (not thereWasOne)
cout << 0 << endl;
else
cout << endl;
return 0;
}
void add(TVec& a, int n)
{
bool isPrime(int);
if (isPrime(n)) {
int i = 0;
while (a[i].n != 0 and a[i].n != n)
++i;
if (a[i].n == 0)
a[i].n = n;
else
a[i].rep = true;
}
}
bool isPrime(int n)
{
bool isP = false;
if (n > 1) {
int d = 2;
while (n % d != 0)
++d;
isP = d >= n;
}
return isP;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment