473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Effecient Prime Number Sum until Max Number

Hi,

I have gone through a lot of pain in optimizing and making this program efficient both with respect to space and time.

1st I implemented Sieve of Eratosthenes instead of conventional trial division method.

Then implemented bit array storage to use less memory.

I wanted to know how can I further improve the performance of this program.

Expand|Select|Wrap|Line Numbers
  1.     memset(sp, 0, BITNSLOTS(maxp));
  2.     for(i = 2; i < maxp; i++) {
  3.         if(!BITTEST(sp, i)) {
  4.             sum+=i;
  5.             for(j = i + i; j < maxp; j += i)
  6.                 BITSET(sp, j);
  7.         }
  8.     }
  9.     printf("%llu\n",sum);
  10.  
Jan 23 '12 #1

✓ answered by Banfa

By definition the code I gave increased the cyclomatic complexity of the code. Generally speaking any (or at least many) optimisation would since you are looking for special cases and then doing something different for them (i.e. executing some control statement).

Since cyclomatic complexity basically counts execution paths and what I suggested added an if statement and therefore an additional execution path the cyclomatic complexity went up.

If you want to reduce the cyclomatic complexity then remove the optimisation.

5 2142
Banfa
9,065 Expert Mod 8TB
Line 4: not clear what the purpose of this is

Line 5: You effectively initialise j to i * 2 but you can initialise it to i * i. Any multiple of i less than i must have been covered by a previous iteration.
Jan 23 '12 #2
Line 4: It sums all the prime number being generated ie: 'i' will be having the prime number

Line 5: changing to i*i is giving wrong answer.
Jan 23 '12 #3
Banfa
9,065 Expert Mod 8TB
Line 5: You also need to change < to <= and you need to test first to make sure that i*i does not overflow the variable, i.e. only perform the loop if i <= maxp/i
Jan 23 '12 #4
@Banfa
Any idea on how to decrease the Cyclomatic Complexity of this code ?? The previous code gives Cyclomatic Complexity of 4 while after making changes on your advice its giving Cyclomatic Complexity of 5..

And the speed improved a bit upon the changes but I want to decrease the Cyclomatic Complexity..
Jan 23 '12 #5
Banfa
9,065 Expert Mod 8TB
By definition the code I gave increased the cyclomatic complexity of the code. Generally speaking any (or at least many) optimisation would since you are looking for special cases and then doing something different for them (i.e. executing some control statement).

Since cyclomatic complexity basically counts execution paths and what I suggested added an if statement and therefore an additional execution path the cyclomatic complexity went up.

If you want to reduce the cyclomatic complexity then remove the optimisation.
Jan 24 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: cpptutor2000 | last post by:
Could some C++ guru please help me with this problem? Suppose I have a string representation of a very large number as: char *strNum = "1234"; now suppose I want to store this number with each...
2
by: wudoug119 | last post by:
This is my code and it will take any number that I input and say it is a prime number. Please help me... int Prime(int prime) //declares isPrime as a function using integers { ...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
2
by: QHorizon | last post by:
Hello, I'm new to Python (I've learned everything up to iterators so far) and fairly new to Programming. This would be my first real program: #Coordinate Geometry (The whole program is not...
5
by: veki | last post by:
Hello, What shoud I change in this code to check, if some number is december: #include <iostream> #include <stdio.h> using namespace std; int main(){
3
by: Nagarjuna502 | last post by:
Hai, I am having 3 questions, 1.How To find harddisk number in C# 2.How To find CPU number in C# 3.How to find IP address of the system in C# ...
3
by: ambi21hs | last post by:
i need a validation code in c for employee number, only number should be allowed can any one help me out with dis..
3
by: Richa Chhabra | last post by:
Is there any validation on character case as well in C++
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.