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

  • Home
  • SEARCH
  • 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 62445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:28:37+00:00 2026-05-10T18:28:37+00:00

I have a range of random numbers. The range is actually determined by the

  • 0

I have a range of random numbers. The range is actually determined by the user but it will be up to 1000 integers. They are placed in this:

vector<int> n 

and the values are inserted like this:

srand(1);  for (i = 0; i < n; i++)   v[i] = rand() % n; 

I’m creating a separate function to find all the non-prime values. Here is what I have now, but I know it’s completely wrong as I get both prime and composite in the series.

void sieve(vector<int> v, int n) {   int i,j;    for(i = 2; i <= n; i++)      {         cout << i << ' % ';         for(j = 0; j <= n; j++)            {               if(i % v[j] == 0)                  cout << v[j] << endl;            }      } } 

This method typically worked when I just had a series of numbers from 0-1000, but it doesn’t seem to be working now when I have numbers out of order and duplicates. Is there a better method to find non-prime numbers in a vector? I’m tempted to just create another vector, fill it with n numbers and just find the non-primes that way, but would that be inefficient?

Okay, since the range is from 0-1000 I am wondering if it’s easier to just create vector with 0-n sorted, and then using a sieve to find the primes, is this getting any closer?

void sieve(vector<int> v, BST<int> t, int n) {   vector<int> v_nonPrime(n);   int i,j;   for(i = 2; i < n; i++)       v_nonPrime[i] = i;    for(i = 2; i < n; i++)      {          for(j = i + 1; j < n; j++)            {               if(v_nonPrime[i] % j == 0)                  cout << v_nonPrime[i] << endl;            }      } } 
  • 1 1 Answer
  • 2 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. 2026-05-10T18:28:38+00:00Added an answer on May 10, 2026 at 6:28 pm

    In this code:

    if(i % v[j] == 0)   cout << v[j] << endl; 

    You are testing your index to see if it is divisible by v[j]. I think you meant to do it the other way around, i.e.:

    if(v[j] % i == 0) 

    Right now, you are printing random divisors of i. You are not printing out random numbers which are known not to be prime. Also, you will have duplicates in your output, perhaps that is ok.

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

Sidebar

Related Questions

No related questions found

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.