print the prime factor of an integers in power form ( ^ ) - c++

I need a program in C++ or C that will calculate prime factor, for Example you input 135 I want an output to be like this (3^3)(5^1) instead of 3,3,3,5.
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
void get_divisors(int n);
int main()
{
int n = 0;
cout << "Enter a number:";
cin >> n;
get_divisors(n);
cout << endl;
}
void get_divisors(int n)
{
int i;
double sqrt_of_n = sqrt(n);
for (i = 2; i <= sqrt_of_n; i++)
if (n % i == 0)
{
cout << i << ", ";
get_divisors(n / i);
return;
}
cout << n;
}

Use std::map<int, int> to contain the prime number (first integer) and the number of occurances of that prime number (second integer).
So when you have a prime, find the value in the map and if it is not in the map, insert it.
If the prime already exists, increment the count.
When printing, you iterate through the map, printing the prime number, then the '^' character, then the count of primes.
Details left as an exercise for the reader.

Related

C++ program to find prime numbers in a fibonacci series

Can someone help me in this?
This is a c++ program that I need to find prime numbers of fibonacci series.
the question says that after you enter the n ( the number of fibonacci series ) the program has to extract the prime numbers from it, and then, if the sum of those prime numbers is an odd number, it has to show 'A' and if it's even, it should show 'D'. The problem is I know how to find both fibonacci series and prime numbers, but I can't merge them.
And I need to keep the code as simple as possible
Can someone help me in this?
This one is for fibonacci series:
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
int n, a = 1, b = 1, c;
cin >> n;
cout << a << endl;
cout << b << endl;
int i = 2;
while (i < n)
{
c = a + b;
cout << c << endl;
a = b;
b = c;
i++;
}
getch();
}
And this one is for prime numbers :
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
int a, b = 1, r = 1, c = 0, x, m;
cout << "please enter number :";
cin >> a;
while (b <= a) {
m = a;
x = b;
while (x != 0) {
r = m % x;
m = x;
x = r;
}
if (m == 1){
c++;
b++;
}
cout << c;
getch();
}
I have to put them together so i can extract the prime numbers in the fibonacci, but I dont know how.
And, I need it to show the prime numbers but my code shows how many numbers are prime
One solution could be to make a function to find the n'th fibonacci number, another function to test if a number is prime, and then loop over the fibonacci number to find the primes
#include <iostream>
int fib(int n) { /* code */ }
bool is_prime(int n) { /* code */ }
int sum_of_prime_in_fib(int lim)
{
int sum = 0;
// loop over the fibonacci number to sum the primes
for (int i = 1; i <= lim; ++i) {
int f = fib(i);
if (is_prime(f))
sum += f;
}
return sum;
}
int main()
{
int n;
std::cout << "please enter number :";
std::cin >> n; //remember to check for errors, which I wont do
std::cout << "the sum of primes is: " << sum_of_prime_in_fib(n) << "\n";
}
I have left the implementation of the two functions as an exercise
Since you did not include conio.h content, I will pose a solution from bare-bone.
This question revolves around several stages.
get all fibonacci sequence in a list
use fibonacci sequence to find if any prime numbers exist (put them in a list)
calculate the sum of the prime numbers from that list
if the sum is even, print "A" otherwise print "D".
The code can be broken down to several functions to make it easier and more intuitive.
#include <iostream>
#include <conio.h>
#include <vector>
using namespace std;
vector<int> generate_fibonacci(int n){
int a = 1, b = 1, c;
//cout << a << endl;
//cout << b << endl;
int i = 2;
vector<int> f;
f.push_back(a);
f.push_back(b);//assuming that the a,b are part of the fibonacci
while (i < n)
{
c = a + b;
f.push_back(c);
a = b;
b = c;
i++;
}
return f;
}
bool is_prime(int n)
{
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
vector<int> find_prime_from_f(vector<int> v){
vector<int> fp;
for(int i: v){
if(is_prime(i))
{
fp.push_back(i);
}
}
return fp;
}
int main(){
cout << "please enter the number of fibonacci" << endl;
int n;
cin >> n;
vector<int> fibonacciNumber = generate_fibonacci(n);
vector<int> fibonacciPrime = find_prime_from_f(fibonacciNumber);
int sum;
for(int i: fibonacciPrime)
{
sum += i;
}
if(sum %2 != 0) cout << "A" <<endl; //if it is odd
else cout << "D" <<endl; //if it is even
}
You can of course tailor some of the details of these functions, including adding some bound checking for negative numbers, and optimize the algorithm by using array, doing the calculation and checking on the fly, or simplify the code a little. However, this should serve as a starting point for your further implementation.

C++ program to find the closest number in the Fibonacci sequence

I need to write a program in C++ to find the closest Fibonacci number to the input number. I patched together a program that tells the Fibonacci number in the nth place, I've included it's code. Next is this task of finding the closest number to the input. What I've gathered is that I probably need to use a binary search/decision tree to rule out the bad numbers. I can generate the Fibonacci numbers so I'm that close. I need a point in the right direction to get me going. I'm pretty sure Binet's formula is involved as well as the golden ratio.
Here's the code for my original program:
int fib(int n)
{
if (n <= 1)
return (n);
return fib(n - 1) + fib(n - 2);
}
int main()
{
cout << "Enter a number greater than -1 and see what the n'th place in the fibbonaci sequence equals.\n";
cin >> n;
fib(n);
if (n >= 0)
cout << "n'th Fibonacci number is " << fib(n) << "\n";
else
cout << "\nInvalid number.\n\n";
return 0;
}
So I found some code that calculated the index for me. I made minor adjustments for input.
#include <iostream>
using namespace std;
int findIndex(int n)
{
if (n <= 1)
return n;
int a = 0, b = 1, c = 1;
int res = 1;
while (c < n)
{
c = a + b;
res++;
a = b;
b = c;
}
return res;
}
int main()
{
int fib_number;
cout << "Please enter a single integer number to see the closest index in the Fibonacci sequence.\n";
cin >> fib_number;
int result = findIndex(fib_number);
cout << "The Fibonacci index is " << result << ".";
}

How to prevent a final cout in a loop from being executed?

I am trying to write a code which gives the prime factorization of given integer.
Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
void primefactor(int a);
int main()
{
int n;
cout<<" Enter the value of n "<<endl;
cin>>n;
primefactor(n);
return 0;
}
void primefactor(int a){
while(a%2==0){
cout<<"2*";
a/=2;
for(int i=3; i<=sqrt(a); i+=2){
while(a%i==0){
cout<<i<<"*";
a=a/i;
}
}
if(a>2){
cout<<a<<endl;
}
}
however when i run the output at the last factor i am getting an additional * in factorization. How can I remove this?
You can use
if (a != 2)
cout<<"2*";
else
cout<<"2";
instead of cout<<"2*";
I had the same issue and solved it with a goto statement.
#include <iostream>
#include <math.h>
using namespace std;
void primeFactorization(int number) {
cout << number << ": ";
// WHILE number is even
while (number % 2 == 0) {
// SET number = number / 2
number = number / 2;
// PRINT 2
cout << 2 << " " ;
// END WHILE
}
// GOTO TERMINAL
reloop:
// FOR factor in 3 to the sqrt(number), by 2
for (int factor = 3; factor <= sqrt(number); factor = factor + 2) {
// IF number modular factor equals 0 THEN
if (number % factor == 0) {
// PRINT factor
cout << factor << " " ;
// SET number = number / factor
number = number / factor;
// GOTO INITIAL
goto reloop;
// END IF
}
// END FOR
}
//IF number > 2 THEN
if (number > 2) {
//PRINT number
cout << number;
//END IF
}
cout << endl;
}
int main() {
int usersNumber;
bool userWants2Play = true;
while (userWants2Play) {
cout << "Please enter a number to be factored: " ;
cin >> usersNumber;
primeFactorization(usersNumber);
cout << "Do you want to play again? 1 or 0: " ;
cin >> userWants2Play;
}
return 0;
}
The goto statement allows for the new odd number to go through the loop again. This lets the odd prime factors print and allows the new old number to be factored again, if the factor exists.

C calculating sum correctly

I can get the sum every time the user inputs an integer until either a negative number or non-integer is inputted. Problem is my sum calculations are off. I.E user putting 1000; sum outputs 1111, then user inputs 2000, it adds up to 3333. Just any advice is appreciated. I'll still experiment around with my coding.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int j , i = 0, k = 0,number;
double sum = 0;
cout << "Enter Positive integer number: ";
while(cin >> number)
{
cout << endl;
if( number < 0)//test if the number is negative
{
cout << "Ending program since user has input a negative number" <<endl;
break;
}
int temp = number;
int p = 1;
while( temp > 0) //counting number of digits
{
sum = sum+temp; //Sum attempt.
temp /= 10;
p *= 10;
i++;
}
cout << sum << endl;
j = i % 3;
p /= 10;
while( i > 0 )//display integer number with 1000 seperator
{
//this is giving me error
cout << char ((number/p) +'0');
number %= p;
p /= 10;
i--;
k++;
j--;
if ((k % 3 == 0 && i > 0)||(j == 0 && i > 2) )
{
cout <<",";
k = 0;
}
}
cout << endl << endl;
cout << "This program will exit if you input any non-integer characters\n";
cout << "Enter another integer number: ";
}
return 0;
}
It looks like you're trying to output an integer number with commas inserted at 1000 boundaries. ie: 1000000 would be displayed as 1,000,000.
This being the case, the easiest way to approach it might not be involving maths but simply to get a string representation of the int (atoi() for example) and count through that. From the back, count forward three chars, insert a comma, repeat until you run out of string.
The specifics of string handling are left as an exercise for the reader - looks like it's his homework after all. ;-)

checking if number is a palindrome in C++

/*
david ballantyne
10/10/13
assesment lab 2
*/
//Libraries
#include <iostream>
//Global Constants
//Functioning Prototypes
using namespace std;
int main() {
int n, num, digit, rev = 0;
cout << "Enter a positive number"<<endl;
cin >> num;
num=n;
do{
digit = num%10;
rev = (rev*10) + digit;
num = num/10;
}
while (num!=0);
if (n == rev)
cout << " The number is a palindrome"<<endl;
else
cout << " The number is not a palindrome"<<endl;
return 0;
}
I enter a palindrome and it keeps telling me it's not a palindrome, also if you could help me understand what kind of loop I would use to ask a "would you like to try again y/n" I'd be grateful.
probably be easier to convert the number to a string. create a new string that is reverse order of the first string. And then compare to see if they are the same. Really no reason to do any real math, Palindromes are lexical, not mathematical.
cin >> num;
num=n;
assigns a user-specified integer to num then replaces it with the value of the uninitialised variable n
Did you mean to reverse the assignment
n=num;
instead?
also if you could help me understand what kind of loop I would use to
ask a "would you like to try again y/n"
You could use a do...while loop with the condition for the while being calculated after you report whether the number was a palindrome.
#include <iostream>
using namespace std;
int main()
{
int userNum, palindrome[100], rem, rem2, count=0, count2=0, compare,
compare2;
bool flag;
cout << "Enter number to test for Palindrome: ";
cin >> userNum;
compare = userNum;
compare2 = userNum;
// counting the digits in the number.
do {
rem = compare % 10;
count += 1;
compare /= 10;
} while (compare >= 1);
// inputing in an array.
for (int i=0; i<count; i++)
{
rem2 = compare2 % 10;
palindrome[i] = rem2;
compare2 /=10;
}
// Comparing array with palindrome.
for (int i=0; i < count; i++)
{
if (palindrome[i] != palindrome[count-i-1])
count2 = 1;
}
if (count2 == 1)
cout << "Not a palindrome.";
else
cout << "Palindrome\n";
return 0;
}