Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef unsigned int uint;
- uint two_arrays(uint N){
- uint data[N];
- bool mask[N];
- unsigned int total = 0;
- // Initialise data
- for(uint i=0; i<N; i++){
- data[i] = i;
- mask[i] = false;
- }
- // Actual computations
- for (uint i=2; i<N; i++){
- if (mask[i] == false){
- for (uint j=2*i; j<N; j+=i){
- mask[j] = true;
- }
- }
- }
- // Count the number.
- for (uint i=2; i<N; i++){
- if (mask[i] == false)
- total+=data[i];
- }
- return total;
- }
- uint array_of_structs(uint N){
- struct {
- unsigned int value;
- bool mask;
- } numbers[N];
- unsigned int total = 0;
- // Initialise data
- for(uint i=0; i<N; i++){
- numbers[i].value = i;
- numbers[i].mask = false;
- }
- // Actual computations
- for (uint i=2; i<N; i++){
- if (numbers[i].mask == false){
- for (uint j=2*i; j<N; j+=i){
- numbers[j].mask = true;
- }
- }
- }
- // Count the number.
- for (uint i=2; i<N; i++){
- if (numbers[i].mask == false){
- total+=numbers[i].value;
- };
- }
- return total;
- }
Advertisement
Add Comment
Please, Sign In to add comment