Looking Bitwise solution for this - c++

You’re given a read only array of n integers. Find out if any integer occurs more than n/3 times in the array in linear time and constant additional space.
If so, return the integer. If not, return -1.
If there are multiple solutions, return any one.
Example :
Input : [1 2 3 1 1]
Output : 1
1 occurs 3 times which is more than 5/3 times.
I've solved this problem and found some solutions for this on google. But I want a bitwise approach. If you could help me, I'd appreciate it.
Solution without bitwise:
int Solution::repeatedNumber(const vector<int> &A)
{
int len = A.size();
if (A.size() == 0)
{
return -1;
}
if (A.size() == 1)
{
return A[0];
}
int c1 = A[0];
int c2 = A[1];
int c1count = 0;
int c2count = 0;
for(int num: A)
{
if(c1 == num)
{
c1count++;
}
else if(c2 == num)
{
c2count++;
}
else if(c1count == 0)
{
c1 = num;
c1count = 1;
}
else if(c2count == 0)
{
c2 = num;
c2count = 1;
}
else
{
c1count--;
c2count--;
}
}
c1count = 0;
c2count = 0;
for(int num : A)
{
if(c1 == num)
{
c1count++;
}
else if(num == c2)
{
c2count++;
}
}
if(c1count > len/3)
{
return c1;
}
else if(c2count > len/3)
{
return c2;
}
else
{
return -1;
}
}

Related

Floating point exception: 8 on Smith number check

So I have written a code to check if a long int number is Smith, but I keep getting Floating point exception: 8, no matter what size do I fix my variables in. Not quite sure what I am missing.
#include <iostream>
bool isPrime(long int k) {
if (k == 1) return false;
for (long int i = 2; i*i < k; i++)
if (k % i == 0)
return false;
return true;
}
int main(){
long int n;
std::cin >> n;
long int sumPr = 0, sumCif = 0;
while (n > 0) {
sumCif += n % 10;
n = n/10;
}
for (long int i = 0; i*i<=n/2; i++) {
if (isPrime(i)) {
while (n % i == 0){
long int p = i;
while (p > 0) {
sumPr += (p % 10);
p = p/10;
}
n = n/i;
}
}
}
if (sumPr == sumCif) std::cout << "1" ; else std::cout << "0";
return 0;
}
The limits of this loop appear to be flawed:
for (long int i = 0; i*i<=n/2; i++) {
Possibly partly due to copy and paste from isPrime(). But the larger problem is you need to modularize this code so that you can properly test each component. And reuse modules (e.g. you implement sum of digits of a number twice in your code.) Code duplication is a potential source of error.
#include <iostream>
bool isPrime(long number) {
if (number < 2) {
return false;
}
if (number % 2 == 0) {
return (number == 2);
}
for (long divisor = 3; divisor * divisor <= number; divisor += 2) {
if (number % divisor == 0) {
return false;
}
}
return true;
}
long sum_digits(long number) {
long sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
long sum_prime_factor_digits(long number) {
long sum = 0;
for (long divisor = 2; divisor <= number; divisor++) {
if (isPrime(divisor)) {
while (number % divisor == 0) {
sum += sum_digits(divisor);
number /= divisor;
}
}
}
return sum;
}
bool is_smith(long number) {
if (isPrime(number)) {
return false; // only composites can play this game
}
return sum_digits(number) == sum_prime_factor_digits(number);
}
int main() {
long number;
std::cin >> number;
if (is_smith(number)) {
std::cout << "1";
} else {
std::cout << "0";
}
std::cout << "\n";
return 0;
}
TESTS
> ./a.out
4
1
> ./a.out
5
0
> ./a.out
6
0
> ./a.out
22
1
> ./a.out
4937775
1
> ./a.out
15966114
1
>
Writing clean code isn't something you do after the fact, it's what you do to help you in the debugging process.

Check if number is perfect prime recursively c++

I've been assigned to write a program that checks if a number is perfect prime or not (the sum of its digits is prime, the sum of the sum of its digits is prime...). I've stumbled upon two extreme cases that break my program:
INPUT: 20328307 OUTPUT: true (expected false)
INPUT: 587899597 OUTPUT: true (expected false)
The code:
#include <iostream>
using namespace std;
bool is_prime(int n) {
if (n == 0 or n == 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i == n; i += 2) {
if (n % i == 0) return false;
}
return true;
}
int sum_of_digits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
bool is_perfect_prime(int n) {
if (sum_of_digits(n) >= 10) is_perfect_prime(sum_of_digits(n)); //cas recursiu
return is_prime(n); //cas base
}
int main() {
int n;
while (cin >> n) cout << (is_perfect_prime(n) ? "true" : "false") << endl;
}
I can't see where this script fails for these two values, and why it doesn't fail for smaller numbers.
First of all your for loop is incorrect, it should be instead:
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
otherwise you return true almost on every non even number. Second you ignore result of recursive call, possible solution is:
bool is_perfect_prime(int n) {
if ( n >= 10 and not is_perfect_prime(sum_of_digits(n)) )
return false;
return is_prime(n); //cas base
}
Finally I've got it to work. The problem was in the is_prime() for loop and in the recursive case of is_perfect_prime(). This is what I've come up with:
bool is_prime(int n) {
if (n == 0 or n == 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
return true;
}
int sum_of_digits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
bool is_perfect_prime(int n) {
if (n < 10) return is_prime(n);
if (! is_prime(n)) return false;
return is_perfect_prime(sum_of_digits(n));
}
Thanks for your answers.

Prime testing algorithm not working properly

I have a prime testing algorithm, which I got from Project Euler, but it returns false when 43 is passed as input. The pseudo code was given in the overview, which I converted into c++ code. I may have made a mistake in converting the pseudo code. What is the actual problem with the algorithm?
#include <iostream>
#include <cmath>
using namespace std;
bool is_prime(int n)
{
if(n <= 1)
{
return false;
}
else if(n < 4)
{
return true;
}
else if(n % 2 == 0)
{
return false;
}
else if(n < 9)
{
return true;
}
else if(n % 3 == 0)
{
return false;
}
else
{
int r = sqrt(n);
int f = 5;
while(f <= r)
{
if(n % f == 0)
{
return false;
}
if((n + 2) % f == 0)
{
return false;
}
f = f + 6;
}
return true;
}
}
int main()
{
cout << is_prime(43);
system("PAUSE");
return 0;
}
Your algorithm is right but here is following mistake
if((n + 2) % f == 0) //wrong
{
return false;
}
should be
if(n%(f+2) == 0) //Right
{
return false;
}
You have an algorithm error. Instead of
(n+2)%f
it should read
n%(f+2)
Others have pointed out your error. You can simplify your code somewhat, reducing all those if statements:
bool is_prime(int n)
{
if(n <= 1)
{
return false;
}
if(n % 2 == 0)
{
return n == 2;
}
if(n % 3 == 0)
{
return n == 3;
}
// Check for factors of 5, 7, ...

True/False function if a sum exists

I want to have a program that returns true when there exist such positive integer numbers a and b such that a*a+b*b=n*n.
My code is:
bool c(int n){
int b=1;
int a=1;
for (a=1; a<=n; a++) {
for (b=a; b<=n; b++) {
if (a*a + b*b == n*n) {
return true;
else
return false;
}
}
}
However this code does not return what i want it to. What can i do to fix that?
Please everybody before you down vote please explain what could be improved in this question. I have noticed that sometimes people have a tendency to down vote questions without any explanation.
Your program doesn't compile. You probably intended to write this:
bool c(int n) {
int b = 1;
int a = 1;
for (a = 1; a <= n; a++) {
for (b = a; b <= n; b++) {
if (a*a + b*b == n*n)
return true;
else
return false;
}
}
}
which compiles, but which is wrong.
But you probably want this:
bool myfunction(int n) {
int b = 1;
int a = 1;
for (a = 1; a <= n; a++) {
for (b = a; b <= n; b++) {
if (a*a + b*b == n*n)
return true;
}
}
return false;
}
int main() {
if (myfunction(5)) {
printf("myfunction(5) returned true\n");
}
}
bool c(int n){
int b=1;
int a=1;
for (a=1; a<=n; a++) {
for (b=a; b<=n; b++) {
if (a*a + b*b == n*n) {
return true;
else
return false;
}
}
}
The code you have written will check for a=1 and b=1 and that evaluates to 1 + 1 equals to 2.
So,all that your code does is compare 2 to n.
The return false statement should be outside both the for loops.
This means that for all combination of a and b there exists no pair (a,b) such that aa + bb = n*n.

the biggest common divisor of 2 numbers using arrays

How could I find the biggest common divisor of 2 numbers using array? I tried to solve it using 2 arrays and I couldn't finish it. How could I improve this program?
#include <iostream>
using namespace std;
int main()
{
unsigned int A[2][10], B[2][10], a, b, c_exp, d, i1, P, x;
bool apartine = false;
cout << "a="; cin >> a;
cout << "b="; cin >> b;
P = 1;
c_exp = 0;
i1 = 0;
while (a % 2 == 0)
{
c_exp++;
a = a/2;
}
if (c_exp != 0)
{
A[i1][0] = 2;
A[i1][1] = c_exp;
i1++;
}
d = 3;
while (a != 1 && d <= a)
{
c_exp=0;
while (a % d == 0)
{
c_exp++;
a = a/d;
}
if (c_exp!=0)
{
A[i1][0] = d;
A[i1][1] = c_exp;
i1++;
}
d = d+2;
}
cout << "\nMatricea A contine:";
for (int i = 0; i < i1; i++)
{
cout << "\n";
for (int j = 0; j < 2; j++)
cout << A[i][j] << ",";
}
c_exp = 0;
i1 = 0;
while (b % 2 == 0)
{
c_exp++;
b = b/2;
}
if (c_exp != 0)
{
B[i1][0] = 2;
B[i1][1] = c_exp;
i1++;
}
d = 3;
while (b != 1 && d <= b)
{
c_exp = 0;
while (b % d == 0)
{
c_exp++;
b = b/d;
}
if (c_exp != 0)
{
B[i1][0] = d;
B[i1][1] = c_exp;
i1++;
}
d = d+2;
}
cout << "\nMatricea B contine:";
for (int i = 0; i < i1; i++)
{
cout << "\n";
for (int j = 0; j < 2; j++)
cout << B[i][j] << ",";
}
return 0;
}
From now on I have to find if the first number of first array exist in the second array and after this I have to compare the exponents of the same number of both array and the lowest one I have to add it to product. After this I have to repeat the same proccess with the second number to the last one of the first array. The problem is that I don't know how to write this.I have to mention that this program isn't complete.
Any ideas?
If you need better solution then you can avoid array and use the below logic.
int main()
{
int a =12 ,b = 20;
int min = a>b ? a:b; // finding minimum
if(min > 1)
{
for (int i=min/2; i>1; i--)//Reverse loop from min/2 to 1
{
if(a%i==0 && b%i==0)
{
cout<<i;
break;
}
}
}
else if(min == 1)
{
cout<<"GCD is 1";
}
else
cout<<"NO GCD";
return 0;
}
You can also check the working example Greatest Common Divisor
I am not quite sure what you are trying to achieve with your code. It looks over complicated. If I were to find the biggest common divisor of two numbers I would do something like the following:
## This is not a correct implementation in C++ (but close to it) ##
Read the two integers **a** and **b**
int max_div(int a, int b){
int div = a > b ? a : b;
while (div != 1 && (a%div != 0 && b%div != 0)){
div--;
}
return div;
}
This function starts with the minimum of a and b as the highest possible common divisor and then works its way backwards until one of two possible outcomes:
It finds a common divisor (a%div == 0 and b%div == 0)
It reaches one (always a common divisor)
EDIT : Now returns one if no higher divisor is found. (Was returning zero which made no sense)