User:Amamprayil/sandbox

The fastest algorithm to find the factors of a number is as follows. void factors(unsigned long n) { unsigned long a,i; int j = 0; for(i = 2; i <= (n/2);++i) {		if(n % i == 0) {			a = n/i; ++j; if(i > a)				return; else if(i == a)			{ --j; printf("\n%d. %lu",j+1, a); return; }			else {				printf("\n%d. %lu  %d.  %lu",j,a,j+1,i); ++j; }			if(j % 38 == 0) wait; }	} }

The following Algorithm gives Fibonacci Numbers up to place value 87 on an ordinary 16 bit compiler.

void fibonacci(void) {	long double i = 0,k = 0,l = 0; int j = 0,m = 1; clrscr; cout << j << ")  " <<  i << "\n";	++j;	++m;	++i;	cout <<  j << ")  " << i << "\n"; ++j; ++m; cout << j << ")  " << i << "\n";	k = i;	++j;	++m;	for(j <= 87 ;++j,++m)	{		l = k;		k = i;		i = k + l;		printf("%d) %0.0Lf\n",j, i); if(m % 24 == 0) {			wait; clrscr; }	} }

--

A palindrome. Add a character and obtain another palindrome.


 * 1) define EOS '\x0'
 * 2) define Esc '\x1b'

/* Pointer Version */

void  convertedstring(char *s,int ch) // *s is the palindrome and ch is the added charater. {	int k = 0,i = 0; while(s[i] != EOS) ++i; k = i;	i += 2; s[i] = EOS; --i; s[i] = ch; --i; --k; for(i;i > 0;--i,--k) s[i] = s[k]; s[i] = ch; }
 * 1) define EOS '\x0'
 * 2) define Esc '\x1b'

/* Pointer Version */

void  convertedstring(char *s,char *str,int ch) {	*str = ch; *str++; while(*s != EOS) *str++ = *s++; *str++ = ch; *str++ = EOS; } --

On a 32 bit compiler the int is stored in 4 bytes. On GCC-GNU 32 bit compiler, instead of int a short is acceptable because it is stored in 2 bytes.

The following function will be applicable.

unsigned short int  key(void) {   short ch1,ch2; unsigned short ch;

ch1 = getch; if(ch1 == 0 || ch1 == 224) {       if(ch1 == 224) ch1 = 0; ch2 = getch; ch = ch2 << 8; ch |= ch1; }   else ch = ch1; return ch; }