-4

I'm trying to write a program showing 10000 digits of PI number, easy, huh? Well, there's a catch, the file is limited to 5000 Bytes. I tried doing things like changing "11" to a to make the code shorter, but it was about 9000 and that's too much for me. Any ideas or tips? And I can't use any other libraries or functions to download the PI number online, or from the file. Just iostream.

honzix
  • 23
  • 3
  • 2
    You can use various methods to compress the digits, but then you wouldn't really be "showing 10000 digits of PI". It isn't clear what you actually need to do. – interjay Sep 23 '14 at 16:37
  • Smells like homework, the task was I guess to figure out some at least very basic compression. 4 bits is more than enough to encode decimal 1 digit. – Andrey Sep 23 '14 at 16:38
  • What code do you already have? – DavidT Sep 23 '14 at 16:38
  • BCD? ...................... – Martin James Sep 23 '14 at 16:38
  • 1
    The current record holder for most digits of pi ever computed wrote a smaller, simpler version of his record-breaking program and put it in the public domain. Might be of help: https://github.com/Mysticial/Mini-Pi. – R. Martinho Fernandes Sep 23 '14 at 16:38

3 Answers3

1

You can pack two digits into one byte, using the upper and lower 4 bits. For for example, for 3.141.... pack the 14 into a byte as 00010100 (0001==1, 0100==4). This way your 10,000 digits will take up 5000 bytes.

When you read the file just print 3. followed by the unpacked data.

Sean
  • 60,939
  • 11
  • 97
  • 136
0

This is the first 10,000 digits of pi: http://www.nerdparadise.com/math/reference/pi10000/

The break down of these by digit is:

0: 968
1: 1026
2: 1021
3: 974
4: 1012
5: 1046
6: 1021
7: 970
8: 948
9: 1014

Which means a minimal encoding for this is:

000: 5
001: 1
010: 2
011: 6
100: 9
101: 4
1100: 3
1101: 7
1110: 0
1111: 8

For a total of 4232.5 bytes of packed data.

I'll leave implementing the decoder as an exercise to the reader.

See also this youtube video for an explanation on how to create minimal encodings:

https://www.youtube.com/watch?v=M5c_RFKVkko

OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
0

It seems like it would be easier just to make a program that generates pi rather then through compressing pi itself. This is done through series such as the Gregory-Leibniz or Nilakantha series. An article on how they work can be found here. These programs can be very easily written in practically any language.

Hope this helps.

Ethan Coe
  • 47
  • 1
  • 4