Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6687813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:19:56+00:00 2026-05-26T05:19:56+00:00

Here is what I have so far: //project Eular Problem 4: Prime Factors #include<iostream>

  • 0

Here is what I have so far:

//project Eular Problem 4: Prime Factors
#include<iostream>
#include<cmath>
typedef unsigned long long int uint_64;
using namespace std;

void storeFactors(uint_64 factors[], uint_64 num)
{
    for(uint_64 i=0;i*i<num;i++
       factors[i]=1;       //assign 1 to all the values

    for(uint_64 j=2; j*j<num;j++){
       if(num%j!=0)
          factors[j]=0;       //assign 0 to non-factors
    } 
}  

//Sieve of Eratosthenes to generate primes
void gen_primes(uint_64 arr[],uint_64 firstElement, uint_64 lastElement, uint_64 size)
{
    for(uint_64 i=0;i<size;i++)   //assigning 1 to all the values
       arr[i]=1;

    for(uint_64 i=2;i*i<=lastElement;i++){   //loop until the square-root of n
       if(arr[i])
          for(uint_64 j=i;j*i<=lastElement;j++)      //eliminate multiples by assigning them 0
            arr[j*i]=0;
       if(firstElement==1)
          arr[firstElement]=0;
    }
}

void arrayComp(uint_64 factors[],uint_64 primeArray[], uint_64 size)
{
    for(uint_64 i=2; i<=size; i++){
        if(factors[i] && primeArray[i]){
                cout<<i<<endl;
        }
    }
}

void processFactors(uint_64 num)
{
    uint_64 size = sqrt(num);
    uint_64 *factors = new uint_64[size];
    uint_64 *primeArray = new uint_64[size];

    storeFactors(factors, num);
    gen_primes(primeArray, 2, num, size);
    arrayComp(factors, primeArray,size);

    delete [] factors;
    delete [] primeArray;
}

int main()
{
    uint_64 number;
    cout<<"Enter a number: "<<endl;
    cin>>number;
    cout<<"The prime factors of "<<number<<" are: "<<endl;
    processFactors(number);

    return 0;
}

I tried taking the sieve approach to calculate the factors. All the non factors are assigned 0. ArrayComp displays the numbers if they are both factors of the input and also prime.

The problem I’m having is that the output is not complete and the programs runs into a segmentation fault. For example, factors for 10 are 5 and 2, but it shows the same answer for 100.

EDIT: Another thing I’m not too confident of is the size of the array. This program shows 3 as the prime factor of 21 and not 7, but if I increase the size by 1, it shows 3 and 5(incorrect)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:19:57+00:00Added an answer on May 26, 2026 at 5:19 am

    The prime factors of 100 are the same as the prime factors of 10, only the multiplicity is different.

    It appears that your representation doesn’t have any way to store repeated factors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a total newb to LINQ. Here is the code I have so far:
Here is what I have tried thus far: > cd /Applications/MAMP/bin/php5/bin/ > ./pear channel-discover
I really wonder why this question hasn't popped up here so far. I have
This is fairly 'math-y' but I'm posting here because it's a Project Euler problem,
Just wondering what little scripts/programs people here have written that helps one with his
Since upgrading to 2008 I and many people here have noticed that randomly VS
Here I have: Public Structure MyStruct Public Name as String Public Content as String
Here I have a password field: /*********************PASSWORD**********************/ $password = new Zend_Form_Element_Password('password'); $alnumValidator = new
Here I have an arbitrary IEnumerable<T> . And I'd like to page it using
Around here we have been working with a bunch of Visual Source Safe repositories

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.