Click here to Skip to main content
       

Algorithms

 
GeneralRe: Specify numbers as product of Primes? Pinmembercp987630-Jun-08 3:18 
GeneralRe: Specify numbers as product of Primes? PinmemberArash Partow30-Jun-08 4:27 
GeneralRe: Specify numbers as product of Primes? PinmemberArash Partow30-Jun-08 2:17 
Questionlogic of prob function in ms excel Pinmembersumit703425-Jun-08 0:40 
QuestionFactorials PinmemberIan Uy24-Jun-08 8:17 
AnswerRe: Factorials PinmemberTim Craig24-Jun-08 10:29 
GeneralRe: Factorials PinmemberIan Uy25-Jun-08 6:32 
GeneralRe: Factorials PinmemberTim Craig25-Jun-08 8:55 
GeneralRe: Factorials PinmvpLuc Pattyn25-Jun-08 11:01 
GeneralRe: Factorials PinmemberTim Craig25-Jun-08 21:06 
AnswerRe: Factorials Pinmembercp987624-Jun-08 12:33 
AnswerRe: Factorials PinmemberMatthew Butler24-Jun-08 12:38 
GeneralRe: Factorials Pinmembercp987624-Jun-08 14:05 
GeneralRe: Factorials PinmvpLuc Pattyn24-Jun-08 15:02 
Hi Peter,
 
IMO this will behave much better; it gives 4 for 10000000, does not overflow,
has no loops but uses a recursion (scaling down by a factor of 5).
 
private int rightmostNonzeroInFactorial(int n) {
	int result = 1;
	// factor ending on 1
	// no change
	// factor ending on 2
	int count2 = (n+8) / 10;
	// factor ending on 3
	int count3 = (n + 7) / 10;
	// factor ending on 4
	int count4 = (n + 6) / 10;
	// factor ending on 5
	int count5 = (n + 5) / 10;
	// factor ending on 6
	int count6 = (n + 4) / 10;
	// factor ending on 7
	int count7 = (n + 3) / 10;
	// factor ending on 8
	int count8 = (n + 2) / 10;
	// factor ending on 9
	int count9 = (n + 1) / 10;
	// factor ending on 0
	int count10 = (n + 0) / 10;
	count5 += count10;
	if (count5 != 0) {
		result = (result * rightmostNonzeroInFactorial(count5)) % 10;
		count2 -= count5;
	}
	count2 += 2 * count4;	// 4 = 2*2
	count2 += 3 * count8;	// 8 = 2*2*2
	count2 += 4 * count6;	// 16 = 2*2*2*2

	count3 += 2 * count9;	// 9 = 3*3;
	count3 += 3 * count7;	// 27 = 3*3*3

	int[] fact3 = new int[] { 1, 3, 9, 7 };	// 3^5 ends on 3
	result = (result * fact3[count3 % fact3.Length]) % 10;
 
	int[] fact2 = new int[] { 1, 2, 4, 8 };	// 2^5 ends on 2
	int factor2=fact2[count2 % fact2.Length];
	if (factor2 == 1 && count2 != 0) factor2 = 6;	// 2^4 = 16
	result = (result * factor2) % 10;
 
	return result;
}
 
Main principle is factors that are not multiples of 5 don't care about their
digits except the lowest one (and also: there will be more powers of 2 than
powers of 5).
 
Smile | :)
 
Luc Pattyn [Forum Guidelines] [My Articles]

Voting for dummies? No thanks. Dead | X|


GeneralRe: Factorials Pinmembercp987624-Jun-08 20:09 
GeneralRe: Factorials PinmvpLuc Pattyn24-Jun-08 23:53 
GeneralRe: Factorials Pinmembercp987625-Jun-08 2:27 
GeneralRe: Factorials PinmvpLuc Pattyn25-Jun-08 2:34 
GeneralRe: Factorials Pinmembercp987625-Jun-08 2:40 
AnswerRe: Factorials Pinmembertim_one14-Jul-08 8:37 
AnswerRe: Factorials PinmemberArash Partow25-Jun-08 2:14 
GeneralRe: Factorials PinmemberIan Uy25-Jun-08 6:26 
QuestionHow to determine the next x,y coordinate for a tank in a 2-D game... PinmemberEdmundisme18-Jun-08 8:35 
AnswerRe: How to determine the next x,y coordinate for a tank in a 2-D game... Pinmember73Zeppelin18-Jun-08 11:38 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... PinmemberMatthew Butler18-Jun-08 11:44 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... Pinmember73Zeppelin18-Jun-08 22:00 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... PinmemberEdmundisme18-Jun-08 12:00 
AnswerRe: How to determine the next x,y coordinate for a tank in a 2-D game... PinmemberMatthew Butler18-Jun-08 11:41 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... PinmemberEdmundisme18-Jun-08 12:01 
JokeRe: How to determine the next x,y coordinate for a tank in a 2-D game... PinmvpCPallini18-Jun-08 21:19 
QuestionPolyline offset algorithm Pinmemberbeko16-Jun-08 21:59 
AnswerRe: Polyline offset algorithm PinmemberAlan Balkany17-Jun-08 4:07 
GeneralRe: Polyline offset algorithm Pinmemberbeko17-Jun-08 4:43 
QuestionRe: Polyline offset algorithm Pinmvp CPallini17-Jun-08 7:03 
AnswerRe: Polyline offset algorithm Pinmemberbeko17-Jun-08 8:26 
GeneralRe: Polyline offset algorithm Pinmembercp987617-Jun-08 12:10 
AnswerRe: Polyline offset algorithm PinmemberArash Partow18-Jun-08 1:22 
GeneralRe: Polyline offset algorithm Pinmemberbeko18-Jun-08 19:55 
QuestionWhat's the period of (cos ax)(sin bx)? Pinmembersherifffruitfly14-Jun-08 10:00 
AnswerRe: What's the period of (cos ax)(sin bx)? Pinmembercp987614-Jun-08 19:03 
AnswerRe: What's the period of (cos ax)(sin bx)? PinmemberMarkBrock15-Jun-08 2:00 
AnswerRe: What's the period of (cos ax)(sin bx)? Pinmvp CPallini15-Jun-08 22:02 
QuestionQuaternion to Euler Pinmember Xmen 11-Jun-08 3:08 
AnswerRe: Quaternion to Euler Pinmembercmk11-Jun-08 11:27 
GeneralRe: Quaternion to Euler Pinmember Xmen 11-Jun-08 12:48 
AnswerRe: Quaternion to Euler PinmemberMark Churchill12-Jun-08 18:38 
QuestionGraphical Object drawing algorithms PinmemberMonty29-Jun-08 2:59 
AnswerRe: Graphical Object drawing algorithms Pinmember 73Zeppelin9-Jun-08 3:11 
GeneralRe: Graphical Object drawing algorithms PinmemberMonty29-Jun-08 3:24 
GeneralRe: Graphical Object drawing algorithms Pinmember 73Zeppelin9-Jun-08 3:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.8.141030.1 | Last Updated 9 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid