Pergunta

I need to compare an integer in a loop to the quotient of a long and a long. in order to not do integer division, if I understand correctly do I need to convert one of the longs into a double?

long prime = primes[d];
int i = 1;

//  "inputNumber / prime" should not be integer division, while it is now.
//  How do I do this yet still compare it to an "int" afterwards? 
while (i < (inputNumber / prime))
{
    primes[i*prime] = 0;
    i++;
}

That's the code snippet. primes is an array filled with longs. Btw is this code correct:

primes[i*prime] = 0;

because I am worried that a long * int won't work for an array index.

Thanks so much!

Foi útil?

Solução

You can multiply one of the operands of the integer division by 1.0 to avoid integer truncation of the result.

Outras dicas

Why not while((i * prime) < inputNumber) instead? A long multiplied by an int results in a long...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top