• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
kaktusas2598

Performance issues in simple program

10 posts in this topic

Hello, I am working on some problems on Project Euler and in one problem it asks you to Write a program which computes biggest prime factor of number. I have written program in C, what finds all primes up to entered number, divides that number by primes and shows biggest prime factor. It is working just fine, bet when I entered bigger numbers like million and so on(and number in Project Euler problem is really big) it crashes. I think it might have issues with perfomance of program and my algorithm efficiency. This is my code:
[source lang="cpp"]//Searches for number prime factors and return biggest prime factor 2012-10-23(24)
#include <stdio.h>
#include <stdlib.h>

int isPrime(int num); // checks if number is prime: 1 - prime 0 - non-prime
void setPrimeArray(int * arr, int num); // composes array of primes up to num to test factors
int * primeFactors(int * arr, int num);//finds prime factors of number
int biggestPrimeFactor(int * factors,int size);//finds biggest prime factor
int setArray (int * arr); // deletes 0 from array and returns setted array(using dynamic reallocation)

int main()
{
int num = 0;
int * numFactors = 0;
int numOfFac = 0;
int i = 0;

printf("Enter the number whose prime factors you want to find: \n");
scanf("%i", &num);

int * primeArray = calloc(num,sizeof(int));
setPrimeArray(primeArray,num);//optimize this function
int s = setArray(primeArray);
printf("Prime numbers up to %i:\n",num);
for(;i < s; i++)printf("%i ",primeArray[i]);

i = 0;
// primeArray -=3;
numFactors = primeFactors(primeArray, num);
numOfFac = setArray(numFactors);
printf("\nPrime Factors of %i is:\n",num);
for(;i < numOfFac; i++)printf("%i ",numFactors[i]);

printf("\nThe biggest prime factor of %i is: %i\n",num, biggestPrimeFactor(numFactors,numOfFac));

system("PAUSE");
free(primeArray);
free(numFactors);
return 0;
}

int isPrime(int num)
{
int factor = 0;
if (num == 1) return 1;
int i = 1;
for (; i <= num; i ++)
{
if( num % i == 0) factor++;
}
if (factor <= 2) return 1;
else return 0;
}

void setPrimeArray(int * arr, int num)
{
int i = 1;
int j = 0;
for(; i <= num; i++)
{
if(isPrime(i))
{
arr[j] = i;
j++;
}
}

}

int * primeFactors(int * arr, int num)
{
int i = 1;//not 0 - do not include prime factor 1, starts from 2
int j = 0;//for indexing primeFactors array;
int * factors = calloc(num, sizeof(int));//HERE WAS ERROR - MUST USE DYNAMIC MEMORY WITH FACTORS
for(;i < num;)
{
if (num % arr[i] == 0) //
{
factors[j] = arr[i]; //BIG, BIG
//*factors = arr[i]; //
//factors++; //
j++; // MISTAKE...
num /= arr[i]; //
i = 1; //
}
else i++;
if (num == 1) return factors;
}
return factors;
}

int biggestPrimeFactor(int * factors,int size)
{
int i = 0;
int max = factors[0];
for(;i < size; i ++)
{
if (factors[i] > max) max = factors[i];
}
return max;
}

int setArray (int * arr)
{
int i = 0;
while(*arr != 0)
{
arr++;
i++;
}
arr = realloc(arr, sizeof(int)*i);
return i;
}
[/source]

And I think I need optimize this function:
[source lang="cpp"]void setPrimeArray(int * arr, int num)
{
int i = 1;
int j = 0;
for(; i <= num; i++)
{
if(isPrime(i))
{
arr[j] = i;
j++;
}
}
}[/source]
Because it takes a lot of time with bigger numbers in this function. It makes an array of primes up to numbers, so I can later use it to find prime factors of number. So if anyone have ideas how I can optimize my code, please share them with me.
0

Share this post


Link to post
Share on other sites
Yea that's going to be extremely slow for large numbers. You're reserving memory for num integers when really you only need memory proportional to the number of prime factors of num at the end of the day. So if you entered 1,000,000 you're looking at almost 4 megs of RAM reserved, roughly 797 [url="http://en.wikipedia.org/wiki/Page_(computer_memory)"]pages[/url]. As you iterate through all that, your swapping pages constantly from CPU cache to memory and visa versa. That's generally a slow(ish) operation.

While your solution certainly works, it's not terribly efficient overall in terms of memory (as you've seen). My suggestion is to go back to the drawing board and see if you can figure out how to calculate the primes without requiring so much memory. You're almost there, you're just being a little overzealous with calloc...
1

Share this post


Link to post
Share on other sites
memory allocation isn't the problem, since that extra memory will never be used, will just sit there.
The problem is that there is so far between the big primes, and you test way too many numbers.
You could cut it in half if you only test odd numbers... (i+=2) but there is probably smarter ways...

But even better, since prime numbers never change, why not precalculate them once, save to file, and then load the prime table from file instead? Edited by Olof Hedman
2

Share this post


Link to post
Share on other sites
Yea disregard what I said, Olof is correct. You're not accessing the majority of pages so your memory allocation is indeed not causing the problem. I still disagree with the magnitude of the allocation though...
0

Share this post


Link to post
Share on other sites
Thanks for reply, I will change it to test just odd numbers, if it will be still slow, I will have to change the algorithm or i will load prime table from file. Thanks.
0

Share this post


Link to post
Share on other sites
Not really sure how much of a hint you want here but there's an abstract data type very suited to this type of search. I won't name it, but the next paragraph contains problem spoilers (if that's such a thing...):

Each number has a pair of factors that are, themselves, numbers that are either primes or smaller numbers with pairs of factors. If you keep splitting your number this way, eventually you'll have a particular type of data structure containing a bunch of numbers at the very "bottom" (my word, not the actual term typically used) that are all prime. All you have to do at that point is pick the right one...

You don't necessarily need to store factors in a file, though there's no reason not too if you really want to do it.
1

Share this post


Link to post
Share on other sites
Hi. You dont need to calculate all the factors, or check whether they are prime or not. You just need to divide the number starting from 2 until you reach a prime number.
I have written something quickly and it seems to be working.

[source lang="cpp"]

#include <stdio.h>

int main() {
int number;

printf("Enter number: ");
scanf("%d", &number);

for (int i = 2; i < number; ++i) {
while (number % i == 0)
number /= i;
}

printf("Biggest prime: %d\n", number);

return 0;
}

[/source] Edited by aavci
1

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1351188423' post='4993877']
The main algorithmic improvement here is diving only by numbers up to the square root of the number.
...code snippet...
[/quote]

Nice. Here's my version:

[code]
int largestPrimeFactor = 2;
for (int i=2; i<=n; ++i) {
while (n % i == 0) {
n/=i;
largestPrimeFactor = i;
}
}
[/code]
1

Share this post


Link to post
Share on other sites
aavci's code doesn't seem to handle situations where the largest prime divides the number twice (try number=4).

alnite's code is correct, but it will take a very long time to run when n is a large prime, which might make it unsuitably slow for the Project Euler challenge.
1

Share this post


Link to post
Share on other sites
Also, trial division isn't the only way to factorize numbers. Basically, factoring is a difficult problem, and it is in fact infeasible to do it when the prime factors are larger than a few hundred digits. Fortunately, you're not dealing with such large numbers.

I would expect Project Euler to specifically choose a large number to prevent you from using trial division, forcing you to implement a better algorithm. Look into Pollard's rho algorithm, or Pollard's p + 1. There's also a continued fraction algorithm (SQUFOF) which is somewhat arcane, but works. Quadratic sieve is the next step up but is considerably harder to implement. Number Field Sieve (NFS) is state-of-the-art but good luck implementing that, seriously. You can also look into elliptic curve factorization (elliptic curve method or ECM for short) which is pretty good for average-size factors but is a bit tricky to get right and difficult to understand - you need to understand elliptic curves.

For reference, quadratic sieve can factorize a 100-digit semiprime into two 50-digit primes in four hours or so. Trial division would have done the same in, say, a few trillion years.

All of this is probably overkill for 64-bit integers though. Edited by Bacterius
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0