For each number read from the standard input the program should print YES if it is a Tribonacci number and NO otherwise. What am I doing wrong in my program, it prints YES, but it wont print NO when the number is not a tribonacci number. For example when number is 45, it should print no.
Tribonacci number formula
T0=0
T1=1
T2=2
Tn=Tn-1 + Tn-2 + Tn-3 (for n>2)
using namespace std;
bool isTrib(int n) {
if(n==0 || n == 1 || n == 2) {
return true;
}
if(n > 2) {
int trib = isTrib(n-1)+isTrib(n-2)+isTrib(n-3);
if(trib == n) {
return true;
}
}
return false;
}
int main()
{
int n;
while(cin>>n) {
bool result = isTrib(n);
cout << result;
result == 1 ? cout << "YES" << endl : cout << "NO" << endl;
}
return 0;
}
you're mixing two things: actual tribonacci numbers and "true"/"false" answer to question "whether N is tribonacci", for example variable trib in your code can be either 0, 1, 2 or 3, it cannot take any other values, but you're trying to compare it with real number, which is apples to oranges
here is fixed version:
bool isTrib(int n) {
if(n==0 || n == 1 || n == 2) {
return true;
}
int n1 = 0;
int n2 = 1;
int n3 = 2;
int trib = n1 + n2 + n3;
while (trib <= n) {
if (trib == n) return true;
n1 = n2;
n2 = n3;
n3 = trib;
trib = n1 + n2 + n3;
}
return false;
}
int main()
{
int n;
while(cin>>n) {
bool result = isTrib(n);
cout << (result ? "YES" : "NO") << endl;
}
return 0;
}
Related
I wrote this to find out which 10-digit numbers are prime but it stops after showing about 20 numbers at all. Would you help me figure out what's wrong? Or suggesting any other sources? It's important to me as it's my school project.
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
void temp()
{
static long long n1=9999999999 ;
long long n2,n3;
if (n1 <= 1 || n1%2==0|| n1%3==0|| n1%5==0|| n1%7==0|| n1%11==0|| n1%13==0|| n1%17==0|| n1%19==0|| n1%23==0|| n1%29==0|| n1%31==0|| n1%37==0|| n1%41==0|| n1%43==0|| n1%47==0|| n1%53==0|| n1%59==0|| n1%61==0|| n1%67==0|| n1%71==0|| n1%73==0|| n1%79==0|| n1%83==0|| n1%89==0|| n1%97==0)
{
std::cout<< n1 << " not prime\n\n";
n1--;
temp();
}
else
{
n2 = (n1 - 1)/2;
while (n2 > 1)
{
n3 = n1 % n2;
if (n3 == 0)
{
std::cout<< n1 << " not prime\n\n";
}
else
{
n2--;
}
}
std::cout<< n1 << " prime\n\n";
n1--;
temp();
}
}
int main(int argc, char** argv)
{
temp();
return 0;
}
When n1 is 9999999983 it gets stuck inside the while loop until n2 is less or equal to 1
You should look for a more efficient way
while (n2 > 1)
{
n3 = n1 % n2;
if (n3 == 0)
{
std::cout << n1 << " not prime\n\n";
n1--;
temp();
}
else
{
n2--;
}
}
EDIT:
This is what you could do in your case (Found it here and changed it to fit your needs (I found it here and changed it up a bit to fit your needs)
#include <iostream>
using namespace std;
void check() {
int n = 9999999999;
while (n != 0) {
int i, m = 0, flag = 0;
m = n / 2;
for (i = 2; i <= m; i++)
{
if (n % i == 0)
{
cout << "Number is not Prime." << endl;
flag = 1;
break;
}
}
if (flag == 0)
cout << "Number is Prime." << endl;
n--;
}
}
int main()
{
check();
return 0;
}
I need to know if a number has a repeating digit using recursion and return 'yes' or 'no'. I am not allowed to use loops or arrays. This is what I've done untill now with 10 global variables and it works, but I think there is a better way.
#include <iostream>
using namespace std;
int counter0 = 0;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
int counter7 = 0;
int counter8 = 0;
int counter9 = 0;
bool check(int k) {
int p = k % 10;;
if (k < 10) {
return false;
} else {
if (p == 0) {
counter0++;
} else if (p == 1) {
counter1++;
} else if (p == 2) {
counter2++;
} else if (p == 3) {
counter3++;
} else if (p == 4) {
counter4++;
} else if (p == 5) {
counter5++;
} else if (p == 6) {
counter6++;
} else if (p == 7) {
counter7++;
} else if (p == 8) {
counter8++;
} else if (p == 9) {
counter9++;
}
if(counter1>1 || counter2>1 || counter3>1 || counter4>1 || counter5>1 || counter6>1 || counter7>1 || counter8>1 || counter9>1)
{
return true;
}
k=k/10;
check(k);
}
}
int main() {
//cout << "Hello, World!" << std::endl;
int n;
cin >> n;
cout << (check(n) ? "yes" : "no") << endl;
//cout << n/10;
return 0;
}
#include <iostream>
using namespace std;
bool hasRepeatingDigit(int n, int mask)
{
// base case: if we have checked all the digits and didn't find any duplicates, return false
if (n == 0)
return false;
/*
p is the place of the last digit in n (n%10).
A digit can range from 0 to 9.
The place of 0 will be 1 << 0 which is 1.
The place of 1 will be 1 << 1 which is 2.
The place of 2 will be 1 << 2 which is 4.
...
...
The place of 9 will be 1 << 9 which is 512.
*/
int p = 1 << (n % 10);
// if place of p has already been marked then it's a duplicate
if (mask&p)
return true;
// otherwise scrap the last digit (n/10), mark place p and recurse on the remaining digits
return hasRepeatingDigit(n / 10, mask|p);
}
int main()
{
int n;
cin >> n;
cout << hasRepeatingDigit(n, 0) << endl;
}
Recursion problems always have a base case and an recursive case.
The base case is simple: k<11 has no repeated digits.
For the recursive case, k has repeated digits if either:
the lower two digits of k are equal, or
k/10 has repeated digits.
So:
bool check(int k) {
if (k < 11)
return false;
int digit = k % 10;
int next = k / 10;
int digit2 = next % 10;
if (digit == digit2)
return true;
else
return check(next);
// Or in one expression:
// return (digit == digit2) || check(next);
}
first, the code is incorrect...
if you enter n=11 it says 'no' but 1 repeated twice. you can fix it by changing the if statement from if(k < 10) to if(k == 0)
you can get down to the bits level but I can't see how much is useful...
in conclusion, this is the best you can do without arrays...
BUT: if you need to find if a digit repeated twice or more in a row the other answer is perfect
So far I have this code. I'm trying to print prime factorization with exponents. For example, if my input is 20, the output should be 2^2, 5
#include <iostream>
#include <cmath>
using namespace std;
void get_divisors (int n);
bool prime( int n);
int main(int argc, char** argv) {
int n = 0 ;
cout << "Enter a number and press Enter: ";
cin >>n;
cout << " Number n is " << n << endl;
get_divisors(n);
cout << endl;
return 0;
}
void get_divisors(int n){
double sqrt_of_n = sqrt(n);
for (int i =2; i <= sqrt_of_n; ++i){
if (prime (i)){
if (n % i == 0){
cout << i << ", ";
get_divisors(n / i);
return;
}
}
}
cout << n;
}
bool prime (int n){
double sqrt_of_n = sqrt (n);
for (int i = 2; i <= sqrt_of_n; ++i){
if ( n % i == 0) return 0;
}
return 1;
}
I hope someone can help me with this.
You can use an std::unordered_map<int, int> to store two numbers (x and n for x^n). Basically, factorize the number normally by looping through prime numbers smaller than the number itself, dividing the number by the each prime as many times as possible, and recording each prime you divide by. Each time you divide by a prime number p, increment the counter at map[p].
I've put together a sample implementation, from some old code I had. It asks for a number and factorizes it, displaying everything in x^n.
#include <iostream>
#include <unordered_map>
#include <cmath>
bool isPrime(const int& x) {
if (x < 3 || x % 2 == 0) {
return x == 2;
} else {
for (int i = 3; i < (int) (std::pow(x, 0.5) + 2); i += 2) {
if (x % i == 0) {
return false;
}
}
return true;
}
}
std::unordered_map<int, int> prime_factorize(const int &x) {
int currentX = abs(x);
if (isPrime(currentX) || currentX < 4) {
return {{currentX, 1}};
}
std::unordered_map<int, int> primeFactors = {};
while (currentX % 2 == 0) {
if (primeFactors.find(2) != primeFactors.end()) {
primeFactors[2]++;
} else {
primeFactors[2] = 1;
}
currentX /= 2;
}
for (int i = 3; i <= currentX; i += 2) {
if (isPrime(i)) {
while (currentX % i == 0) {
if (primeFactors.find(i) != primeFactors.end()) {
primeFactors[i]++;
} else {
primeFactors[i] = 1;
}
currentX /= i;
}
}
}
return primeFactors;
}
int main() {
int x;
std::cout << "Enter a number: ";
std::cin >> x;
auto factors = prime_factorize(x);
std::cout << x << " = ";
for (auto p : factors) {
std::cout << "(" << p.first << " ^ " << p.second << ")";
}
}
Sample output:
Enter a number: 1238
1238 = (619 ^ 1)(2 ^ 1)
To begin with, avoid using namespace std at the top of your program. Second, don't use function declarations when you can put your definitions before the use of those functions (but this may be a matter of preference).
When finding primes, I'd divide the number by 2, then by 3, and so on. I can also try with 4, but I'll never be able to divide by 4 if 2 was a divisor, so non primes are automatically skipped.
This is a possible solution:
#include <iostream>
int main(void)
{
int n = 3 * 5 * 5 * 262417;
bool first = true;
int i = 2;
int count = 0;
while (i > 1) {
if (n % i == 0) {
n /= i;
++count;
}
else {
if (count > 0) {
if (!first)
std::cout << ", ";
std::cout << i;
if (count > 1)
std::cout << "^" << count;
first = false;
count = 0;
}
i++;
if (i * i > n)
i = n;
}
}
std::cout << "\n";
return 0;
}
Note the i * i > n which is an alternative to the sqrt() you are using.
I need to find the sum of prime numbers between two numbers, say x1 and x2 inclusively, however i cant detect whats wrong?
for example if i entered 3 and 9 i would get 15 but i am getting 133!
#include <iostream>
using namespace std;
int prime(int n1, int n2)
{
int count =0;
bool prime = true;
for (n1; n1 < n2; n1++)
{
for (int i = 2; i < n1; i++)
{
if (n1 % i == 0) {
prime = false;
continue;
}
else
count++;
}
}
return count;
}
int main()
{
int n1, n2;
cout << " Enter values for n1 and n2 (n1 must be smaller than n2): ";
cin >> n1>>n2;
cout << " Sum of prime numbers from " << n1 << " and till " << n2 << " inclusively : " << prime(n1, n2) << endl;
system("pause");
return 0;
}
Your prime function is not appropriate. This should be like.
int prime(int n1, int n2) {
int sum = 0;
for (n1; n1 < n2; n1++) {
bool prime = true;
for (int i = 2; i < n1; i++) {
if (n1 % i == 0) {
prime = false;
break;
}
}
if( prime ) { // current n1 is prime
sum = sum + n1;
}
}
return sum;
}
You are not adding anything if your n1 is prime.
I am a big believer in the one loop per function. This is a very good example for this. Your inner loop checks if a number is prime so really it should be a function on its own:
bool is_prime(int n);
int sum_primes_between(int n1, int n2)
{
int sum = 0;
for (; n1 <= n2; n1++)
{
if (is_prime(n1))
sum += n1;
}
return count;
}
Now not only it's easier to read, but you can better test individual parts of code. You can test that is_prime is correct and after that you can test num_primes_between is correct. If you had gone this route from the start you wouldn't even have had the bug you currently have with detecting if a number is prime.
And here is an even neater solution with range-v3:
using namespace ranges;
bool is_prime(int n);
int sum_primes_between(int n1, int n2)
{
return accumulate(view::ints(n1, n2 + 1) | view::filter(is_prime), 0);
}
None of the answers provided so far respect the requirement of being inclusive.
Here's a corrected version including an optimized algorithm to check for primality:
#include <iostream>
#include <cmath>
using namespace std;
bool is_prime(int n) {
// handle special cases
if (n <= 1) {
return false;
}
if (n <= 3) {
return true;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
/* because we covered multiples of 2 and 3 we can reduce the number of checks
drastically */
for(int i = 5, r = sqrt(n); i =< r; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
int sum_of_primes(int n1, int n2) {
int sum = 0;
// loop from n1 up to n2
for ( ;n1 <= n2; n1++) {
if (is_prime(n1)) {
sum += n1;
}
}
return sum;
}
int main() {
cout<<"The sum of primes between 2 and 15 is: "<<sum_of_primes(2,15)<<endl;
return 0;
}
Your original code didn't calculate the sum of primes, you simply incremented your count variable on every iteration that didn't find a divisor of the current number you were checking.
I have a task to find the count of all prime numbers in random generated sequence.
The input is:
25 10
where 25 is the number s which should be < 10^3 and is used for srand(s) and the number 10 is N(the length of the sequence) which should be < 10^9. The numbers in the sequence are generated with rand()%1000. There are no more than 100 total examples. So here is my code:
#include <iostream>
#include <stdlib.h>
using namespace std;
bool isPrime(int n)
{
if (n == 0 || n == 1) return false;
for (int i = 2; i < n; i++)
{
if (n % i == 0) return false;
}
return true;
}
void Solve(int s, int N)
{
if (s >= 1000 || s <= 0 || N > 1000000000 || N <= 0)
{
cout << "s or N out of bounds!" << endl;
return;
}
int number,
count = 0,
i;
srand(s);
for (i = 1; i <= N; i++)
{
number = rand() % 1000;
if (isPrime(number))
{
count++;
}
}
cout << count << endl;
}
int main()
{
int s, N;
int counter = 0;
while (cin >> s >> N)
{
if (++counter == 100)
{
cout << "You have reached the maximum examples(100)!" << endl;
break;
}
Solve(s, N);
}
return 0;
}
And when I input 25 10 the answer should be 3 but I got 1. Where is my problem? Any help will be appreciated.