The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
9 views
The following function computes an array $SPF$, where, for any integer $1 < i < 1000$,  $SPF[i]$ is the smallest prime factor of $i$. For example, $SPF[6]$ is $2$, and $SPF[11]$ is $11$.

There are five missing parts in the following code, commented as $/* Blank */$. For each of them, copy the entire line with the comment and fill the blank appropriately in your answer sheet.

int SPF[1000];

void findSPF() {

SPF[1] = 1;

// Initializing SPF of every number to be itself

for (int i = 2; i < 1000; i++) { _____; /* Blank 1 */

// SPF of every even number is 2

for (int i = 4; i < 1000; i += 2) {

SPF[i] = _____; /* Blank 2 */ }

// For odd numbers, updating the SPFs of their multiples

for (int i = _____; i * i < 1000; i++) {

/* Blank 3 */

if (SPF[i] == i) {

// No smaller factor of i found yet

for (int j = _____; j < 1000; j+= i) {

/* Blank 4 */ if (SPF[j] == j) {

SPF[j] = _____; /* Blank 5 */

}

}

}

}

}
asked in Programming by Boss (40.4k points) | 9 views

Please log in or register to answer this question.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,540 questions
54,099 answers
187,269 comments
71,006 users