Skip to content

Instantly share code, notes, and snippets.

@harshraj22
Last active January 19, 2020 03:53
Show Gist options
  • Select an option

  • Save harshraj22/9a849d33311ffae820c0ef7c224d28f9 to your computer and use it in GitHub Desktop.

Select an option

Save harshraj22/9a849d33311ffae820c0ef7c224d28f9 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b);
int main(){
int test=1;
while(test--){
int i,k=0,n;
bool possible = true;
cin>>n;
vector<int> vec(n,0);
for(int i=0;i<n;i++){
cin >> vec[i];
k = gcd(vec[i],k);
}
for(i=0;i<n;i++){
vec[i]=vec[i]/k;
// remove all 2s
while(vec[i]%2==0)
vec[i]=vec[i]/2;
// remove all 3s
while(vec[i]%3==0)
vec[i]=vec[i]/3;
// if it contains anything else,
if(vec[i]!=1)
possible = false;
}
if(possible)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
int gcd(int a,int b){
if(a==0)
return b;
else return gcd(b%a,a);
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
// returns true if 'x' is a perfect square
bool isSquare(int x){
// inbuilt function 'sqrt' returns a double, which is typecasted into int
int sq = sqrt(x);
// so, sqrt(3) = 1, but 1*1 != 3
if(sq*sq == x)
return true;
else
return false;
}
void solve(){
int x; cin >> x;
// for each num in the range, check if it is a perfect square
for(int i=1;i<=x;i++){
if(isSquare(i))
cout << i << " ";
}
cout << '\n';
}
int main(){
int test; cin >> test;
while(test--)
solve();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment