Prime Generating Algorithm

A Prime Number can be divided evenly only by 1, or itself. And it must be a whole number greater than 1.
For instance:- Input : 7
Output: is prime ( 7 has no factors other than 1 and 7 itself)
Input : 8
Output: is not prime ( 8 has factors: 2,4,1,8)

1. Function isprime(value)
2. if value modulus 2 == 0
3.    return false
4. i = 3
5.  while i < square root (value)
6.    If value modulus i == 0
7.        return false
8.        i = i +2
9.    end while
10. return false
11. end isprime

Armstrong Number

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
For Instance:-
Input: 153
Output: is an Armstrong number
note: 111=1 , 555=125 , 33*3=27 . 1+125+27=153 ie the number itself

Function Armstrong( n )
1. a = n 
2. sum = 0
3. while ( n > 0 )
4.    i = n % 10
5.    prod = i * i * i
6.    sum = sum + prod
7.    n = n / 10
8.    end while
9. if( sum == a )
10.    return true 
11. else
12.     return false
13. end Armstrong

Reverse Digits

To put digits of a number in reverse order
For instance:-
Input : 3217
Output: 7123

1. Function digits_in_reverse( n )                              
2. while( n > 0 )
3.    i = n%10      // % returns the remainder when numbers are divided
4.    print( i )
5.    print( “ ” )
6.    n = n / 10
7.    end while
8. end digits_in_reverse

Palindrome Number

A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed.
For Instance :-
Input: 1221
Output: is palindrome(reverse = 1221)
Input: 1231
Output: is not palindrome(reverse = 1321)

Function Palindrome_number ( n )
1. a = n
2. r = 0
3. while ( a > 0 )
4.    i = a % 10
5.    r = r  * 10 + i
6.    a = a / 10
7.    end while 
8. If( n==r )
9.    return true
10. else
11.    return false
12. end Palindrome_number    

Factorial of a Number

Factorial of a number is the product of all integers such that 1<= integer <=number. If the number is ‘n’ then its factorial is represented as n!.
For instance :-
Input : 5
Output: 120 (factorial(5) = 5 * 4 * 3 *2 *1 = 120)

Function Factorial( n ) //where n is the number who’s factorial is required
1. f = 1                                                                     
2. while( n>1 )
3.    f = f *n
4.    n = n-1
5.    end while
6. print f
7. end Factorial

Fibonacci Series

A series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc.

1. Function Fibonacci( n )  // where n is the number of fibonacci numbers you want
2. a = 0  // initial state of fibonacci is 0
3. b = 1                                     
4. i = 1
5. while( i < n)
6.    if( i==1 )
7.       print( a )
8.       print( “ ” )
9.       continue; 
10.       end if // later part of the loop is skipped 
11.    else if( i==2)
12.       print( b )
13.       print( “ ”)
14.       continue
15.       end if
16.    c = a + b   //fib(n) = fib(n-1) + fib(n-2)
17.    a = b
18.    b = c
19.    print( c )
20.    print( “ ” )
21.    end while
22. end Fibonacci   

Greatest Common Divisor

In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder.
For instance :-
Input: gcd(12 , 8)
Output : 4

1. Function gcd( a , b )
2. if ( b == 0 )
3.    return a
4.    end if
5. r = a % b
6. return gcd( b , r )
7. end gcd  

Sum of cube of n natural numbers with loop

Input: 3
Output : 36 (111 + 222 +333 = 36)

1. Function sum_cube( n )
2. i = 1
3. sum = 0
4. while( i < n )
5.    sum = sum + (i*i*i)
6.    i++
7.    end while
8. return sum  
9. end sum_cube

Sum of cube of n natural numbers without loop

Input: 3
Output : 36 (111 + 222 +333 = 36)

1. Function sum_of_cube(  n )
2. sum = (n*n*(n+1)*(n+1))/4
3. return sum
4. end sum_of_cube

Swap two numbers without third variable

Input : a =3 , b=4
Output: a=4 , b=3

1. Funciton swap( num1 , num2 )
2. num1 = num1 + num2
3. num2 = num1 - num2
4. num1 = num1 - num2
5. end swap

BACK TO THE TOP