Generating n prime numbers in JavaScript

/**

* Prime number
* parameter n is an integer
* Generate n prime numbers
*/
var primeList=[2]; //seed
function prime(n)
{
n += 1;
for (var k = 0; k < primeList.length; k++)
{
if((n%primeList[k])==0)
return prime(n);
}
primeList.push(n);
}

Posted in algorithms, javascript prime number function, prime number, prime number function | Leave a comment

Get fibonacci numbers using JavaScript function

//Returns a Fibonnaci number
//@param n is an integer
function fibonacci(n)
{
if(n < 0)
{
return 0;
}
else if(n <= 2)
{
return 1;
}
else
{
return fibonacci(n - 1) + fibonacci(n - 2)
}
}
Posted in Uncategorized | Leave a comment

Fibonacci numbers : Python

def fibonacci(n):
""" Returns a fibonacci number
Author: http://www.victorbyte.net """
if (n == 1) or (n==2):
return 1;
elif n >= 3:
return fibonacci(n - 1)+fibonacci(n-2);
Posted in introduction, programming | Leave a comment

Find the factorial : Python


def factorial(n):
    """ Returns the factorial of an integer
        Author: http://www.victorbyte.net """
    if n == 0:
        return 1;
    elif n > 0:
        return n * factorial(n - 1);
Posted in Code samples | Tagged | Leave a comment

Remove duplicate characters: python

Python is a great language for text formatting. The trivial functions below are not fully representative of the things one can do in Python, however for a beginner they are sufficient. For further reading I suggest one looks at Python's regular expressions. 

Function #1.
The function takes a lot of overhead, and uses up a lot of resources.
 


def hasDuplicate(str_var):
    """ Checks for and removes repeated characters
        Author: http://www.victorbyte.net
        returns string value """
    iterate = True;
    while iterate:
        iterate = False;
        for i in range (0, len(str_var)):
            for j in range( i + 1, len(str_var)):
                if str_var[i].lower() == str_var[j].lower():
                    str_var = str_var[:j]+str_var[j+1:];
                    iterate = True;
                    break;
    return str_var;

Function #2.
We employ Python's set object. Since sets must not contain duplicate elements, Python removes duplicate characters. This is more efficient than Function #1


def duplicateRmv(str=""):
 setFrmt = set(str);
 str="";
 for i in setFrmt:
  str+=i;
 return str;
Posted in duplicate | Tagged | Leave a comment

Check for duplicate characters : Python

def hasDuplicate(str_var):
    """ Checks for repeated characters
        Author: http://www.victorbyte.net
        returns Boolean value """
    for i in range (0, len(str_var)):
        for j in range( i + 1, len(str_var)):
            if str_var[i].lower() == str_var[j].lower():
                return True;

    return False;
Posted in algorithms, python | 3 Comments

Check for repeated characters: csharp


/// Checks for repeated characters
/// Author: http://www.victorbyte.net
/// returns Boolean value

static bool hasDuplicate(string str)
{
   for (int i = 0; i < str.Length; i++)
   { 
       for (int j = i + 1; j < str.Length; j++)
       {                   
           if (str.ElementAt(i) == str.ElementAt(j))
               return true;               
       }       
   }        
   return false;
} 
Posted in Code samples | Tagged , | Leave a comment

Greatest Common Divisor : Java


/** 
* Gets GCD between two positive integers.   
* source: http://www.victorbyte.net  
* Returns int value   
*/ 
public static int getGCD(int numA, int numB)
{
    if(numA < numB)
    {
        int temp = numA;
        numA = numB;
        numB = temp;
    }

    if( numB <= 0 ) return 0;

    if((numA % numB) == 0)
        return numB;

    return getGCD( numB , numA %= numB );
}
Posted in Code samples | Tagged | Leave a comment

Using Python to visualize the div, mod and remainder of a number.

def divTheory(a,b):
n = a;
d = b;
q = n // d;
r = n % d;
print(n," = number n",d," = divisor n",q," = quotient n",r, " = remaindern");
Posted in Uncategorized | Leave a comment

Check for truth in a predicate : Java


/**
  * Checks for truth in a predicate.
  * http://www.victorbyte.net/
  * Returns boolean value 
  */
       
    public static boolean forEachPredicate(double [] set_S)
    {
        for(double x:set_S)
            if(!(Math.pow(x,2.0) >= x))   //predicate
                return false;
            return true;
    }

    public static boolean negateForEachPredicate(double [] set_S)
    {
        return !forEachPredicate(set_S);
    }

    public static boolean thereExistsPredicate(double [] set_S)
    {
        for(double x:set_S)
            if (( x + 1 ) > x)   // predicate
                return true;
            return false;
    }

    public static boolean negateThereExistsPredicate(double [] set_S)
    {
        return !thereExistsPredicate(set_S);
    }

    public static boolean uniquePredicate(double [] set_S)
    {
        int unique = 0;

        for(double x:set_S)
            if (( x + 1 ) > x)  //predicate
                unique++;

        return unique == 1;
    }
 
Posted in Code samples | Tagged | Leave a comment