Posts

Showing posts from April, 2018
For more Computer Science, Linux, C++ content visit ProgrammerCave
Follow us on Facebook | Instagram | Telegram

Contribute on Paypal

C++: Collatz Conjecture Program

Image
The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half the previous term. Otherwise, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1. [wikipedia] Image source : wolfram                            C++ implementation of Collatz conjecture #include <iostream> #include <cassert> //assert() int main() {     int n;     std::cout << "Enter number (greater than 1)\n" ;     std::cin >> n;     assert(n > 1);     std::cout << "Series is :\n" ;     std::cou...

C++: Find Prime Factors of a Number.

Image
Given a number, we have to find it's prime factors. Prime Factorization is finding which prime numbers multiply together to make the original number. [mathsisfun] You can find many such projects from here . Here is the C++ implementation to find prime factors. #include <iostream> #include <vector> std::vector< int > find_prime_factors( int n) {     std::vector< int > result;     for ( int i = 2; i <= n; i++)     {         while (n % i == 0)         {             result.push_back(i);             n = n/i;         }     }     return result; } int main() {     int n;     std::cout << "Enter number\n" ;     s...
For more Computer Science, Linux, C++ content visit ProgrammerCave
Follow us on Facebook | Instagram | Telegram

Contribute on Paypal