This question would be typical of the kind of exercise set in class, because it would make the student work for an answer, and an answer could not be downloaded by a simple internet search, which was why I made the comment I did. However, I also suspect that the OP is not skilled at searching because I found much helpful material quite quickly, which I'll refer to shortly.
StackOverflow is not a site for people to write code for you. A question of this nature is considered off-topic here. We help you with the code you have, but we do not give you code for nothing. At the moment you do not have an algorithm, or code or math, which is expecting quite a lot of SO. You have to give more yourself to get something back.
If you do not understand the math of generating infinite digit sequences of Pi you can ask in https://math.stackexchange.com/ which is more suited to this task. If you want to discuss class exercises like this one, perhaps go here https://matheducators.stackexchange.com/. If you want a more general chat with programmers go here: https://softwareengineering.stackexchange.com/
OK. Now to answers.
A quick google sent me here:
Su, Francis E., et al. "Finding the N-th digit of Pi." Math Fun Facts.
This cites:
- David Bailey, Peter Borwein, and Simon Plouffe. "On the rapid computation of various polylogarithmic constants", Math. Comp. 66(1997), 903-913.
- Victor Adamchik and Stan Wagon, "A simple formula for pi", Amer. Math. Monthly 104(1997), 852-855.
And uses the formula:
Pi = SUMk=0 to infinity 16-k [ 4/(8k+1) - 2/(8k+4) - 1/(8k+5) - 1/(8k+6) ].
To calculate hexadecimal digits of Pi without storing any previous digits.
This is then easy to code with a simple persistent storage of the current hex digit and a digit count.....
See also related discussion in Efficiently computing the first 20-digit substring to repeat in the decimal expansion of Pi