c++prime-factoring

Generate numbers from 2 to 10,000. The numbers printed can only be a multiple of 2 prime numbers


Homework: I'm just stumped as hell. I have algorithms set up, but I have no idea how to code this

Just to be clear you do not need arrays or to pass variables by reference.

The purpose of the project is to take a problem apart and using Top-Down_Design or scratch pad method develop the algorithm.

Problem:

Examine the numbers from 2 to 10000. Output the number if it is a Dual_Prime.

I will call a DualPrime a number that is the product of two primes. Ad where the two primes are not equal . So 9 is not a dual prime. 15 is ( 3 * 5 ) . The output has 10 numbers on each line.

My Algorithm set-up

Step 1: find prime numbers.:

bool Prime_Number(int number)
{
    for (int i = 2; i <= sqrt(number); i++)
    {
        if (number % 1 == 0)
            return false;
    }
    return true;
}

Step 2: store prime numbers in a array

Step 3: Multiply each array to each other

void Multiply_Prime_Numbers(int Array[], int Size)
{
    for (int j = 0; j < Size- 1; j++)
    {
        Dual_Prime[] =  Arr[j] * Arr[j + 1]  
    }
}

Step 4: Bubble sort

void Bubble_Sort(int Array[], int Size) // Sends largest number to the right
{
    for (int i = Size - 1; i > 0; i--)
        for (int j = 0; j < i; j++)
            if (Array[j] > Array[j + 1])
            {
                int Temp = Array[j + 1];
                Array[j + 1] = Array[j];
                Array[j] = Temp;
            }
}

Step 5: Display New Array by rows of 10

void Print_Array(int Array[], int Size)
{
    for (int i = 0; i < Size; i++)
    {
        cout << Dual_Prime[i] << (((j % 10) == 9) ? '\n' : '\t');
    }
    cout << endl;
}

Solution

  • // Ex10_TwoPrimes.cpp : This file contains the 'main' function. Program execution begins and ends there.
    
    
    #include "pch.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void Homework_Header(string Title);
    void Do_Exercise();
    void Sieve_Of_Eratosthenes(int n);
    void Generate_Semi_Prime();
    
    bool Semi_Prime(int candidate);
    bool prime[5000 + 1];
    
    int main()
    {
        Do_Exercise();
    
        cin.get();
        return 0;
    }
    
    void Do_Exercise()
    {
        int n = 5000;
    
        Sieve_Of_Eratosthenes(n);
    
        cout << endl;
    
        Generate_Semi_Prime();
    }
    
    void Sieve_Of_Eratosthenes(int n)
    {
        // Create a boolean array "prime[0..n]" and initialize 
        // all entries it as true. A value in prime[i] will 
        // finally be false if i is Not a prime, else true. 
    
        memset(prime, true, sizeof(prime));
    
        for (int p = 2; p*p <= n; p++)
        {
            // If prime[p] is not changed, then it is a prime 
            if (prime[p] == true)
            {
                // Update all multiples of p 
                for (int i = p * p; i <= n; i += p)
                    prime[i] = false;
            }
        }
    }
    
    bool Semi_Prime(int candidate)
    {
        for (int index = 2; index <= candidate / 2; index++)
        {
            if (prime[index])
            {
                if (candidate % index == 0)
                {
                    int q = candidate / index;
                    if (prime[q] && q != index)
                        return true;
                }
            }
        }
        return false;
    }
    
    void Generate_Semi_Prime()
    {
        for (int i = 2; i <= 10000; i++)
            if (Semi_Prime(i)) cout << i << "\t";
    }