Ejemplo n.º 1
0
        // CreatePrimeArray will return a Token of TokenType.Vector containing first 'n' primes
        // implemented by c0dejunkie, refined by Kashif Imran
        public static Token CreatePrimesArray3(string operation, List <Token> arguments)
        {
            if (arguments.Count != 1 || arguments[0].TokenType != TokenType.Vector || arguments[0].Count != 1 ||
                arguments[0].FirstValue < 1 || arguments[0].FirstValue != (int)arguments[0].FirstValue)
            {
                return(Token.Error("Invalid argument to function. Please provide a positive integer"));
            }

            int numPrimes = (int)arguments[0].FirstValue;

            double[] primes = new double[numPrimes];

            int          count  = 0;
            long         i      = 2;
            List <Token> args   = new List <Token>();
            Token        number = new Token(TokenType.Vector, i);

            args.Add(number);
            while (count < numPrimes)
            {
                if (Numerical.IsPrime("", args).FirstValue != 0)
                {
                    primes[count] = i;
                    count++;
                }
                number[0] = ++i;
            }

            return(new Token(TokenType.Vector, primes));
        }
Ejemplo n.º 2
0
        // FindPrimeAt will return a Token of TokenType.Vector which will indicate the prime at a given index
        // Originally implemented by c0dejunkie, refined by KashifImran
        public static Token FindPrimeAt2(string operation, List <Token> arguments)
        {
            if (arguments.Count != 1 || arguments[0].TokenType != TokenType.Vector || arguments[0].Count != 1 ||
                arguments[0].FirstValue < 1 || arguments[0].FirstValue != (int)arguments[0].FirstValue)
            {
                return(Token.Error("Invalid argument to function. Please provide a positive integer"));
            }

            // an index of 1 will return 2 as the prime number
            int    primeIndex = (int)arguments[0].FirstValue;
            int    primeCount = 0;
            double primeAt    = 1;

            if (primeIndex == 1)
            {
                primeAt = 2;
            }
            else
            {
                primeCount = 1;
                List <Token> testL       = new List <Token>();
                Token        primeTest   = new Token(TokenType.Vector, 0);
                Token        primeResult = new Token(TokenType.Bool, 0);
                testL.Add(primeTest);
                while (primeCount < primeIndex)
                {
                    primeAt     += 2;
                    primeTest[0] = primeAt;
                    if (Numerical.IsPrime("isprime", testL)[0] == 1)
                    {
                        primeCount++;
                    }
                }
            }
            return(new Token(TokenType.Vector, primeAt));
        }