查找复合数 [英] Finding composite numbers

查看:145
本文介绍了查找复合数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的随机数。范围实际上由用户决定,但最多可以有1000个整数。它们放在这里:

 矢量< int> n 

,值如下插入:

  srand(1); 

for(i = 0; i v [i] = rand()%n;

我创建一个单独的函数来查找所有的非素数值。这是我现在的,但我知道这是完全错了,因为我得到的素材和复合的系列。

  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;
}
}
}

只是有一系列的数字从0-1000,但它似乎并不工作,现在,当我有数字不按顺序和重复。有没有更好的方法来寻找非质数向量?我试着创建另一个向量,用n个数字填充它,只是找到非素数,但是效率会低吗?



好吧,范围是从0-1000我想知道如果更容易创建向量与0-n排序,然后使用筛子找到素数,这是更接近吗?

  void sieve(vector< int> v,BST< int> t,int n)
{
vector< int& v_nonPrime(n);
int i,j;
for(i = 2; i v_nonPrime [i] = i;

for(i = 2; i {

for(j = i + 1; j< n; j ++)
{
if(v_nonPrime [i]%j == 0)
cout< v_nonPrime [i]< endl;
}
}
}


解决方案>

在此代码中:

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

您正在测试索引,看看它是否可以被v [j]整除。我想你应该这样做,换句话说:

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

现在,您正在打印i的随机因子。您不打印出已知不是素数的随机数。此外,你会在你的输出中有重复,也许这是确定。


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;
           }
     }
}

解决方案

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.

这篇关于查找复合数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆