Wrox Programmer Forums Generating Pi to more than 14 decimal places
 Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
 Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
October 27th, 2010, 07:44 PM
 Friend of Wrox Join Date: Jun 2008 Location: Snohomish, WA, USA Posts: 1,652 Thanks: 3 Thanked 141 Times in 140 Posts

Yep. You got it.

But you know, he gives you the C++ code for it. There's no reason you can't use Visual C++, compile that code into a library, and then invoke that library function from VB.

EDIT: No, I take that back. He said he uses a #define to set the number of places of accuracy, so you'd have to change even his code if you wanted to convert it into a callable library with arbitrary precision.

Last edited by Old Pedant; October 27th, 2010 at 07:46 PM.
 The Following User Says Thank You to Old Pedant For This Useful Post: SamC (October 30th, 2010)
October 27th, 2010, 07:44 PM
 Friend of Wrox Join Date: Jun 2008 Location: Snohomish, WA, USA Posts: 1,652 Thanks: 3 Thanked 141 Times in 140 Posts

I hope you noted that he said it took 15 hours on his PC to get 1,000,000 places.
 The Following User Says Thank You to Old Pedant For This Useful Post: SamC (October 30th, 2010)
October 27th, 2010, 07:55 PM
 Authorized User Join Date: Oct 2010 Location: "Great" Britain Posts: 29 Thanks: 23 Thanked 0 Times in 0 Posts

Quote:
 Originally Posted by Old Pedant I hope you noted that he said it took 15 hours on his PC to get 1,000,000 places.
Aye, but that's really not much when you consider the vast amount of calculations involved. I only really wanted to find enough to fill the first 25 lines of the console window, even with the algorithm I was using that should take only a matter of seconds... I think I shall abandon this project and leave it for when I have a better understanding of programming.

In the mean time I think I may make another Prime Generator, this time using the Sieve of Atkin. That should be quite simple right?

PS: If I want to create a .txt file to save the prime numbers in what would be the best way of going about this?
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
October 27th, 2010, 08:08 PM
 Friend of Wrox Join Date: Jun 2008 Location: Snohomish, WA, USA Posts: 1,652 Thanks: 3 Thanked 141 Times in 140 Posts

http://msdn.microsoft.com/en-us/libr...eamwriter.aspx

Code:
Dim outf As New StreamWriter("c:\full\pat\to\yourfile.txt")
outf.WriteLine("the next prime number is " & n )
... repeat...
outf.Close()
Or variations on that theme. Learn to use the MSDN docs. They really are amazingly complete.
 The Following User Says Thank You to Old Pedant For This Useful Post: SamC (October 30th, 2010)
October 27th, 2010, 08:10 PM
 Friend of Wrox Join Date: Jun 2008 Location: Snohomish, WA, USA Posts: 1,652 Thanks: 3 Thanked 141 Times in 140 Posts

Never used Sieve of Atkin.

Used to use Sieve of Erastothenes as a benchmarking program. Can't tell you how many languages I wrote that in. But in those days (1970s, early 80s), we were happy to get the primes up to about 16,000.
 The Following User Says Thank You to Old Pedant For This Useful Post: SamC (October 30th, 2010)
October 27th, 2010, 08:23 PM
 Authorized User Join Date: Oct 2010 Location: "Great" Britain Posts: 29 Thanks: 23 Thanked 0 Times in 0 Posts

Cool, I use Prime95 for stability testing overclocks all the time, I guess it must be generating some pretty huge primes!

Do you think Sieve of Eratosthenes would be a better option then? It is simpler to implement.
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
October 27th, 2010, 08:41 PM
 Friend of Wrox Join Date: Jun 2008 Location: Snohomish, WA, USA Posts: 1,652 Thanks: 3 Thanked 141 Times in 140 Posts

The big limitation back in the day was the amount of memory needed. You need an array element for each number from 1 to N. (Well, we "cheated" by only looking at odd numbers, so we needed half that number of elements, but you get the idea.)

In the BASICs of that day, where many implemented only a single numeric data type which was, of course, floating point, that meant the you were using 4 or 6 or 8 bytes per array element. In a 64KB machine, you usually only had a MAX of 32KB left for variables, so even 32K divided by 4 meant a top end of 8000 array elements.

In a modern machine, you can surely devout a few hundred megabytes to the array *AND* with VB.NET (for example) your array elements can each be only 1 byte long (that is, use an array of BYTE data type). So sure...it's thus trivial to implement and you can get up into the range of 1,000,000,000 as a max prime (esp. if you pull the same trick: Don't use an array element for the even numbers).

Actually, VB.NET is fast enough that if you wanted to get a bit more clever you could extend the range by a factor of 8 by using only 1 BIT per odd number. A little more coding work, but not hard.
 The Following User Says Thank You to Old Pedant For This Useful Post: SamC (October 30th, 2010)
October 28th, 2010, 02:23 PM
 Authorized User Join Date: Oct 2010 Location: "Great" Britain Posts: 29 Thanks: 23 Thanked 0 Times in 0 Posts

Could you modify my code so that it will display 28 decimal places of π please? That would be awesome.

Also how would I write a routine which sorts an array of strings into alphabetical order? It's really bugging me!
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein
October 28th, 2010, 02:31 PM
 Friend of Wrox Join Date: Jun 2008 Location: Snohomish, WA, USA Posts: 1,652 Thanks: 3 Thanked 141 Times in 140 Posts

Quote:
 Originally Posted by SamC Could you modify my code so that it will display 28 decimal places of π please? That would be awesome.
Tell the truth, I've never used DECIMAL with VB before. But I would imagine all you have to do is change all the variables to DECIMAL. Oh, and get rid of Math.Pow(). Just have to do that "by hand" with multiplies, instead.

Quote:
 Also how would I write a routine which sorts an array of strings into alphabetical order? It's really bugging me!
Remember what I said about learning to use MSDN??

http://msdn.microsoft.com/en-us/libr...tem.array.aspx

http://msdn.microsoft.com/en-us/library/6tf1f0bc.aspx

You don't HAVE to "write a routine". The .NET framework has it built in.
 The Following User Says Thank You to Old Pedant For This Useful Post: SamC (October 30th, 2010)
October 28th, 2010, 02:38 PM
 Authorized User Join Date: Oct 2010 Location: "Great" Britain Posts: 29 Thanks: 23 Thanked 0 Times in 0 Posts

I was aware of that function, I wanted to write the routine myself because my tutor had challenged me to do so over half-term. I could get it to work easily for numerical values, but it didn't work for Strings...
__________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction."
- Albert Einstein

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is OffTrackbacks are Off Pingbacks are On Refbacks are Off

 Similar Threads Thread Thread Starter Forum Replies Last Post Number with 2 decimal places only pallone SQL Server 2000 4 July 3rd, 2007 07:29 AM Display Decimal Places rsm42 ASP.NET 1.0 and 1.1 Basics 3 January 7th, 2007 09:46 AM Round to 2 decimal places adman JSP Basics 0 June 30th, 2005 07:24 AM Two decimal places only pallone Javascript How-To 5 February 1st, 2005 11:35 PM output only 2 decimal places Toka1 Javascript How-To 2 February 20th, 2004 10:44 AM

All times are GMT -4. The time now is 04:25 PM.