Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2008 by Ivor Horton; ISBN: 9780470225905
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2009, 06:33 PM
Registered User
 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
Default How does my code addition effect the result?

I'm in Chapter 4, learning about pointers. There is a sample program, Ex4_09.cpp, for calculating and printing out the first 100 prime numbers. This got me curious and instead of printing out the first 100 primes I went for the first 50,000 primes. It took 14 seconds to calculate before printing it out. (I say 14, but that was just me counting seconds.) I then tried the first 1,000,000 but after ten minutes I aborted the progam. I did have to include this line to get it to compile:

Code:
#pragma comment(linker, "/STACK:12345678")
Anyway, I continued reading the book and Ivor mentions that the code would be more efficient if we only divided by primes less than or equal to the square root of the trial number. So I included the cmath library and added a couple of lines:

Code:
    do
    {
        trial += 2; 

        // I added this line:
        long compare = static_cast<long>(sqrt(static_cast<double>(trial)));


        found = 0; // Set found indicator
        for(int i = 0; i < count; i++) // Try division by existing primes
        {
            
            //And I added this line:
                if (*primes+i < compare)


                found = (trial % *(primes + i)) == 0;// True for exact division

                if(found) // If division is exact
                    break; // it’s not a prime
        }
I used static_cast twice so it would compile without warnings. Now this doubled the speed. I counted 7 seconds for 50,000 numbers instead of 14. But it also gave me two less prime numbers. With my added lines the highest prime I got was 611939. But using the code as it is found in the book, I also get 611951 and 611953.

Now the formatting of the numbers is the same. IOW I still have the same number of rows and columns. So, that seems to say that my calculation found two prime numbers less than 611939. Which to me indicates that there is a mistake in my logic. The casting has somehow altered the results.

I'm no mathmetician. Can anyone explain this for me?

Thanks.
 
Old February 23rd, 2009, 07:06 PM
Registered User
 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
Default

After posting this and again looking at my code I realized my condition was specified as "less than" instead of "less than or equal to". So, I changed that and that corrected the results.

But I'm still confused. I had to have 50,000 results, because that's what MAX is equal to. But my faulty results ended with the number 611939. And the correct results ended with the numbers 611939, 611951 and 611953. But since both sets of numbers contained 50,000 members, that means my faulty results had two numbers that the correct results didn't have.

Why would leaving out the = add more numbers instead of leaving some numbers out?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Hexadecimal Addition madanshah16 XSLT 1 July 31st, 2007 07:21 AM
Code running without any result jack123 SQL Server 2000 2 July 11th, 2007 05:02 PM
Please help my code is not giving me a result set yudee JSP Basics 0 September 20th, 2006 09:55 AM
Send code to server and getto=ing back result topshed Classic ASP Professional 1 January 30th, 2006 12:56 AM
weird funtion calling effect/result blur_ivy ASP.NET 1.0 and 1.1 Professional 0 March 6th, 2005 11:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.