Skip to content

Commit

Permalink
primes functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Lee committed Nov 30, 2013
1 parent 8cf0957 commit 154c00b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 44 deletions.
31 changes: 24 additions & 7 deletions src/main/java/com/lastcalc/parsers/math/IsPrimeParser.java
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Lists;
import com.lastcalc.TokenList;
import com.lastcalc.parsers.Parser;
import org.jscience.mathematics.number.LargeInteger;


/**
Expand All @@ -24,7 +25,7 @@
*/
public class IsPrimeParser extends Parser {

private static TokenList template = TokenList.createD(Lists.<Object> newArrayList("isprime"),Integer.class);
private static TokenList template = TokenList.createD(Lists.<Object> newArrayList("isprime"),Number.class);

@Override
public TokenList getTemplate() {
Expand All @@ -36,30 +37,46 @@ public TokenList getTemplate() {
public ParseResult parse(final TokenList tokens, final int templatePos) {

final String op = (String) tokens.get(templatePos);
final Integer input = (Integer) tokens.get(templatePos + 1);
final Integer input = ((LargeInteger)tokens.get(templatePos + 1)).intValue();


if(op.equals("isprime")){

//first, we check if the number is greater than the last element of the primeslist.

//if yes, then we expand the primeslist to be big enough until the last element
// of the primeslist is the largest prime that is still less than the input number. if the input number is prime
// then return true else false


//if the input number is less than the last element of the primeslist, then we see if it is in the primeslist

//if it's there then it's prime, if not the false.


//run the sqrt n primality test algorithm

final Integer inputSqrRoot=(int)Math.sqrt((double)input);

if(input ==1){
return ParseResult.success(tokens.replaceWithTokens(templatePos, templatePos + template.size(), "false"));
return ParseResult.success(tokens.replaceWithTokens(templatePos, templatePos + template.size(), false));
}
else if(input==2 || input==3){
return ParseResult.success(tokens.replaceWithTokens(templatePos, templatePos + template.size(), "true"));
return ParseResult.success(tokens.replaceWithTokens(templatePos, templatePos + template.size(), true));
}

for(int i=2;i<=inputSqrRoot;i++){
if(input%i==0){
return ParseResult.success(tokens.replaceWithTokens(templatePos, templatePos + template.size(), "false"));
return ParseResult.success(tokens.replaceWithTokens(templatePos, templatePos + template.size(), false));
}
}

return ParseResult.success(tokens.replaceWithTokens(templatePos, templatePos + template.size(), "true"));
return ParseResult.success(tokens.replaceWithTokens(templatePos, templatePos + template.size(), true));

}
return ParseResult.fail();
else{
return ParseResult.fail();
}
}

@Override
Expand Down
50 changes: 27 additions & 23 deletions src/main/java/com/lastcalc/parsers/math/PrimesUnderParser.java
Expand Up @@ -30,45 +30,49 @@
*/
public class PrimesUnderParser extends Parser {

//all the primes under 1000. This parser doesnt work for numbers above 1000
final private ArrayList<Integer> allprimes=new ArrayList<Integer>();

private static TokenList template = TokenList.createD(Lists.<Object> newArrayList("primesunder"),Integer.class);


private static TokenList template = TokenList.createD(Lists.<Object> newArrayList("primesunder"),Number.class);

@Override
public TokenList getTemplate() {
return template;
}

public PrimesUnderParser(){//put the primes into the array list

final BufferedReader br;
final String [] primesString;
try{
br = new BufferedReader(new InputStreamReader(
Bootstrap.class.getResourceAsStream("prime_numbers.txt")));
//found in src/main/resources/com/lastcalc/bootstrap/prime_numbers.txt

primesString = br.readLine().split(", ");
}
catch(Exception e){
System.err.println("prime_numbers.txt input error");
return;
}

for(int i=0;i<primesString.length;i++){
allprimes.add(Integer.parseInt(primesString[i]));
}
}


@Override
public ParseResult parse(final TokenList tokens, final int templatePos) {

final String op = (String) tokens.get(templatePos);
final Integer input = (Integer) tokens.get(templatePos + 1);
final Integer input = ((LargeInteger)tokens.get(templatePos + 1)).intValue();

//result.append(new TokenList.SimpleTokenList("42"));
if(op.equals("primesunder")){


final BufferedReader br;
final String [] primesString;
try{
br = new BufferedReader(new InputStreamReader(
Bootstrap.class.getResourceAsStream("prime_numbers.txt")));//src/main/resources/com/lastcalc/bootstrap/prime_numbers.txt

primesString = br.readLine().split(", ");
}
catch(Exception e){
System.err.println("prime_numbers.txt input error");
return ParseResult.fail();
}

ArrayList<Integer> allprimes=new ArrayList<Integer>();

for(int i=0;i<primesString.length;i++){
allprimes.add(Integer.parseInt(primesString[i]));
}


Iterator<Integer> allprimesiterator = allprimes.iterator();

ArrayList<Integer> resultList=new ArrayList<Integer>();
Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/com/lastcalc/bootstrap/bootstrap.txt
Expand Up @@ -115,4 +115,12 @@ Num / List = apply (X=Num/X) to List
Num mod List = apply(X=Num mod X) to List
Num % List = apply(X=Num % X) to List

Num ! = factorial Num
Num ! = factorial Num

is Num a prime= isprime Num
is Num prime ? =isprime Num
is Num prime= isprime Num
is prime Num= isprime Num
primality of Num=isprime Num


24 changes: 12 additions & 12 deletions src/test/java/com/lastcalc/parsers/math/IsPrimeTest.java
Expand Up @@ -34,30 +34,30 @@ public class IsPrimeTest {
public void isPrimeTest(){

final IsPrimeParser ist = new IsPrimeParser();
Parser.ParseResult pr=ist.parse(TokenList.createD("blah", "isprime", Integer.valueOf(3), "blah"),1);
Parser.ParseResult pr=ist.parse(TokenList.createD("blah", "isprime", LargeInteger.valueOf(3), "blah"),1);
Assert.assertTrue("Ensure parse was successful: ", pr.isSuccess());
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", "true","blah"), pr.output);
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", true,"blah"), pr.output);

pr=ist.parse(TokenList.createD("blah", "isprime", Integer.valueOf(5), "blah"),1);
pr=ist.parse(TokenList.createD("blah", "isprime", LargeInteger.valueOf(5), "blah"),1);
Assert.assertTrue("Ensure parse was successful: ", pr.isSuccess());
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", "true","blah"), pr.output);
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", true,"blah"), pr.output);


pr=ist.parse(TokenList.createD("blah", "isprime", Integer.valueOf(10), "blah"),1);
pr=ist.parse(TokenList.createD("blah", "isprime", LargeInteger.valueOf(10), "blah"),1);
Assert.assertTrue("Ensure parse was successful: ", pr.isSuccess());
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", "false","blah"), pr.output);
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", false,"blah"), pr.output);

pr=ist.parse(TokenList.createD("blah", "isprime", Integer.valueOf(29), "blah"),1);
pr=ist.parse(TokenList.createD("blah", "isprime", LargeInteger.valueOf(29), "blah"),1);
Assert.assertTrue("Ensure parse was successful: ", pr.isSuccess());
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", "true","blah"), pr.output);
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", true,"blah"), pr.output);

pr=ist.parse(TokenList.createD("blah", "isprime", Integer.valueOf(100), "blah"),1);
pr=ist.parse(TokenList.createD("blah", "isprime", LargeInteger.valueOf(100), "blah"),1);
Assert.assertTrue("Ensure parse was successful: ", pr.isSuccess());
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", "false","blah"), pr.output);
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", false,"blah"), pr.output);

pr=ist.parse(TokenList.createD("blah", "isprime", Integer.valueOf(7919 ), "blah"),1);
pr=ist.parse(TokenList.createD("blah", "isprime", LargeInteger.valueOf(7919 ), "blah"),1);
Assert.assertTrue("Ensure parse was successful: ", pr.isSuccess());
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", "true","blah"), pr.output);
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", true,"blah"), pr.output);



Expand Down
Expand Up @@ -34,7 +34,7 @@ public class PrimesUnderTest {
public void primesUnderTest(){

final PrimesUnderParser pup = new PrimesUnderParser();
Parser.ParseResult pr=pup.parse(TokenList.createD("blah", "primesunder", Integer.valueOf(3), "blah"),1);
Parser.ParseResult pr=pup.parse(TokenList.createD("blah", "primesunder", LargeInteger.valueOf(3), "blah"),1);
Assert.assertTrue("Ensure parse was successful: ", pr.isSuccess());
Assert.assertEquals("Ensure parse result is what it's supposed to be: ",TokenList.createD("blah", Lists.newArrayList(2,3),"blah"), pr.output);
}
Expand Down

0 comments on commit 154c00b

Please sign in to comment.