wrong function

And so..Need help.
i have string of numbers and i have to print out those pairs of numbers which havent got any other divider,only 1.For example 6 and 25 or 21 and 11.Need to make this work with function.
Where is mistake?

#include<iostream>//nodrošina ievad-izvadierīču lietošanu
using namespace std;//tiks lietots vārdkopu apgabals std objektiem
int savstpirmsk(int sk);

int main()
{
   
int sk;
int *arr;
cout << "LD3" << endl;
cout << "Programma lauj ievadit skaitlu virkni un izvada visus virknes skaitlus,"<<endl; 
cout << "kuri ir savstarpeji pirmskaitli"<<endl;
cout << endl;

cout<<"Cik skaitlus velies ievadit?"<<endl;
cin>>sk;
arr = new int[sk];

for (int i=0;i<sk;i++)
{
    cout<<"Ievadi "<<i+1<<". skaitli "<<endl;
    cin>>arr[i];//skaitļu ievade
    cout<<endl;
}
cout<<"Savstarpeji pirmskaitlu pari ir "<<savstpirmsk(sk)<<endl;

delete[] arr;
cout<<endl;
system("pause");
return 0;

}// main funkcijas beigas

int savstpirmsk(int sk)
{for (int i=0;i<sk-1;i++)
    {for (int j=i+1;j<sk-1;j++)
       {int arr[i];
       int arr2[j];
        
       
       if ((arr[i]*2%arr2[j]!=0)||(arr2[j]*2%arr[i]!=0))
       return arr[i];
       }
    }
}   
You want to find pairs of integers (m, n) such that the greatest common divisor of m and n is 1.
Look up "greatest common divisor" or "gcd". There are algorithms abound. Wikipedia even
gives C code.
That is not exactly what i need.I need coprime/relatively prime numbers.But i dont have idea,how to compare 100 numbers,each to each :(
Two numbers are relatively prime if the only common factor they share is 1. Stated another way,
their greatest common divisor is 1.

To compare every number to every number, you need two nested for loops. Pseudo-code:


for( every number M in the set )
for( every number N in the set such that M != N )
check if (M, N) are relatively prime


2 questions

1)How to make loop work to not compare the same number,as you wrote M!=N.where to put it?

2) if ((arr[i]*2%arr2[j]!=0)||(arr2[j]*2%arr[i]!=0))
i know this isnt way to find out,whether numbers are coprime.Can someone tell the right formula?
1.
Pseudo-code:

if( M != N )
/* compare them */
else
/* don't compare them */


2. *sigh* Congratulations. You are the first person I've ever had to do this for:

http://lmgtfy.com/?q=coprime

first link to Wikipedia.
First sentence on the page:

In mathematics, two integers a and b are said to be coprime or relatively prime if they have no common positive factor other than 1 or, equivalently, if their greatest common divisor is 1.


Click the link on "greatest common divisor".
Scroll down to "Euclid's Algorithm".
Click the link on "Euclidean algorithm".
Scroll down to the section "Implementations".
Translate the pseudo-code to C++.



I give.Too stupid for this.Last i could made..

#include<iostream>//nodrošina ievad-izvadierīču lietošanu
using namespace std;//tiks lietots vārdkopu apgabals std objektiem
int savstpirmsk(int sk,int arr[]);

int main()
{
bool pirmsk=true; 
int sk;
int *arr;
cout << "LD3" << endl;
cout << "Programma lauj ievadit skaitlu virkni un izvada visus virknes skaitlus,"<<endl; 
cout << "kuri ir savstarpeji pirmskaitli"<<endl;
cout << endl;

cout<<"Cik skaitlus velies ievadit?"<<endl;
cin>>sk;
arr = new int[sk];

for (int i=0;i<sk;i++)
{
    cout<<"Ievadi "<<i+1<<". skaitli "<<endl;
    cin>>arr[i];//skaitļu ievade
    cout<<endl;
}
{for (int i=0;i<sk;i++)
    {for (int j=0;j<sk;j++)
if (arr[i]<=arr[j])

{for (int i=0;i<sk;i++)
    {for (int j=0;j<sk;j++)
      {for (int k=2;k<arr[i];k++)
      if ((arr[i]%k==0) && (arr[j]%k==0))
      {pirmsk=false;
      break;
      }
      }
      }
      }
      
cout<<"Savstarpeji pirmskaitlu pari ir "<<savstpirmsk(sk,arr)<<endl;

delete[] arr;
cout<<endl;
system("pause");
return 0;

}// main funkcijas beigas

int savstpirmsk(int sk,int arr[])
 
  { if (pirmsk)                 
 for (int i=0;i<sk;i++)
    {for (int j=0;j<sk;j++)
      
     return arr[i];
      
      
    
       }
    }
}
        
       
       
    
  
Ok, here's the pseudo-code for the simplest version of gcd (copied from Wikipedia)

1
2
3
4
5
function gcd(a, b)
    if b = 0
       return a
    else
       return gcd(b, a mod b)


Can you translate this function to C/C++ code?

Post your attempt.
Topic archived. No new replies allowed.