Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
449 multiplied by 449, repeated 524 times, what number are the last four digits of the result ,may I ask how to use the code to achieve it?

449^524,What are the last four number of the result?
Rob Philpott 3-Nov-11 7:02am    
Project Euler?
PEIYANGXINQU 3-Nov-11 8:09am    
No,it just an interview question,it ask me to make codes to solve it
Amir Mahfoozi 4-Nov-11 1:07am    
Thanks for sharing it with us ;)

Use BigInteger[^]

BigInteger answer = BigInteger.Pow(449, 524);

edit: just for fun
Share this answer
UJimbo 3-Nov-11 7:15am    
Show him how to get the substring containing the last 4 numbers and you get my 5 marks Bob :)
BobJanova 3-Nov-11 7:52am    
I thought giving a complete answer to what's probably a homework or trivia question was a bit much :P. If he can't work out how to do that then he doesn't deserve the answer.
PEIYANGXINQU 3-Nov-11 8:40am    
BigInteger answer = BigInteger.Pow(449, 524);
Console.WriteLine(answer.ToString().Substring(answer.ToString().Length-4, 4));
Thank you!
Roger Allen 3-Nov-11 13:26pm    
Wouldn't this get the last 3 digits and the ',' character?
Which means it would not fulfill the request of the last 4 digits...
BobJanova 3-Nov-11 13:48pm    
I don't think BigInteger's string output has punctuation in it. The output in my solution is from some code I have to do extended precision (I didn't have a BigInteger test project lying around).
I think the underlying question is how to get the result without using an extended precision library.

   449 * 449 = 201601
201601 * 449 = 90518849

but we can see only the four least significant digits are required as
  1601 * 449 = 718849

Coding a suitable loop gives the result in a speedy 8 microseconds!

In response to the comment
The general technique is demonstrated with a small calculation and there is no need to go beyond 449^3 (90518849) as the result is applicable to any power.

The important point is that in each iteration of the calculation we need to carry forward only the last 4 digits of the previous result.

Int32 result = 1;
for (Int32 i = 0; i < 524; ++i) {
  result = (result * 449) % 10000;

Share this answer
PEIYANGXINQU 3-Nov-11 14:35pm    
En,Thank you very much.You way is better than the direct calculation.
What a boring question:
int m = 449;
int r,i;
for(r=m,i=1;i<524;i++) r = (m*r)%1000;
// three digits: 201
for(r=m,i=1;i<524;i++) r = (m*r)%10000;
// four digits: 9201
for(r=m,i=1;i<524;i++) r = (m*r)%100000;
// five digits: 79201
for(r=m,i=1;i<524;i++) r = (m*r)%1000000;
// six digits: 779201

Share this answer
Amir Mahfoozi 4-Nov-11 1:03am    
+5 We have a genius here ;)
mbue 4-Nov-11 7:13am    
Thanks for the candy ;)
Hint: only the last four digits of the result are required.
Share this answer
PEIYANGXINQU 3-Nov-11 9:46am    
Thank you.May be we do not have to calculate the result.
It doesn't get any easier than this.
ReallyBigNumber % 10000
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900