Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.

You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.

A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.

Ad
Ad
Code
Diff
  • package com.mystuff.juststuff;
    
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeParseException;
    import java.util.Set;
    import java.util.TreeSet;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    public class Palindrome {
    
    	private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
    
    	public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
    		return IntStream.range(startDate.getYear(), endDate.getYear() + 1).mapToObj(Palindrome::createPalindrome)
    				.filter(date -> isValidDate(date, startDate, endDate)).collect(Collectors.toCollection(TreeSet::new));
    	}
    
    	private static LocalDate createPalindrome(int year) {
    		String yearStr = String.valueOf(year);
    		String datePalindrome = new StringBuilder().append(yearStr).reverse().append(yearStr).toString();
    		try {
    			return LocalDate.parse(datePalindrome, formatter);
    		} catch (DateTimeParseException e) {
    			return null;
    		}
    
    	}
    
    	private static boolean isValidDate(final LocalDate date, LocalDate startDate, LocalDate endDate) {
    		if (date == null)
    			return false;
    		if (startDate.isEqual(date) || endDate.isEqual(date))
    			return true;
    		return startDate.isBefore(date) && endDate.isAfter(date) ||
    				endDate.isBefore(date) && startDate.isAfter(date);
    	}
    }
    • package com.mystuff.juststuff;
    • import java.time.LocalDate;
    • import java.time.format.DateTimeFormatter;
    • import java.time.format.DateTimeParseException;
    • import java.util.Set;
    • import java.util.SortedSet;
    • import java.util.TreeSet;
    • import java.util.stream.Collectors;
    • import java.util.stream.Stream;
    • import java.time.LocalDate;
    • import java.time.format.DateTimeFormatter;
    • import java.time.temporal.ChronoUnit;
    • import java.util.stream.IntStream;
    • public class Palindrome {
    • private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
    • private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
    • public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
    • final SortedSet<LocalDate> sortedDates = Stream.of(startDate, endDate).collect(Collectors.toCollection(TreeSet::new));
    • return Stream.iterate(sortedDates.first(), date -> date.plusDays(1))
    • .limit(ChronoUnit.DAYS.between(sortedDates.first(), sortedDates.last().plusDays(1)))
    • .filter(Palindrome::isPalindrome)
    • .collect(Collectors.toCollection(TreeSet::new));
    • }
    • private static boolean isPalindrome(final LocalDate date) {
    • final String fwd = date.format(formatter);
    • return fwd.equals(new StringBuilder(fwd).reverse().toString());
    • }
    • public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
    • return IntStream.range(startDate.getYear(), endDate.getYear() + 1).mapToObj(Palindrome::createPalindrome)
    • .filter(date -> isValidDate(date, startDate, endDate)).collect(Collectors.toCollection(TreeSet::new));
    • }
    • private static LocalDate createPalindrome(int year) {
    • String yearStr = String.valueOf(year);
    • String datePalindrome = new StringBuilder().append(yearStr).reverse().append(yearStr).toString();
    • try {
    • return LocalDate.parse(datePalindrome, formatter);
    • } catch (DateTimeParseException e) {
    • return null;
    • }
    • }
    • private static boolean isValidDate(final LocalDate date, LocalDate startDate, LocalDate endDate) {
    • if (date == null)
    • return false;
    • if (startDate.isEqual(date) || endDate.isEqual(date))
    • return true;
    • return startDate.isBefore(date) && endDate.isAfter(date) ||
    • endDate.isBefore(date) && startDate.isAfter(date);
    • }
    • }

The exact same program as before but with some rather messy comments in between (and at the start and end of the program). The Codewars built-in runBF interpreter should correctly handle the program regardless and return the string "Hello World!".

Code
Diff
  • [
      This is a loop comment.
      Command characters, they should be ignored <here>.
      Including [matching square brackets].
      Oh, and "+" and >-< signs.
      And gibberish: skldfjsdklfjs,..sd,dsf,s.fd,++--sdfsdsdfKLSDJFKLDSJ[[[][]][][[[][]]]]
      And unmatched round brackets: ((((()))(((()))))))))))))))))))))))
    ]
    sdklfjsdkldfjskldfs+++++++++++++++++sdlkfjsdlkfjsdfklsdjfkldsjfd++++++++++++++++++SDLKFJSDKLJSDFKLDSFJFDSKLJDFS++++++++++++++++++++++++SDLKFJDSFKLJFSDKLDSFJKSLDFJDSFKLDFSJ+++++++++++++.>++++(+++++(+()++)+((++)(++)))++++++++++++++NESWNEWSNEWWWSEEEEE+++++++++++++++;;++++++++++++;;;;;++++++++++++;;;;+++++++++;;;;;++++++++++++********++++++++++.>+++*****++++++++++++++***;;;*****++++++++++++;;**;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++.+++++sdfsfdsdfsdfsfd++++sdfsdffdsfds++sdfsdffsddfssdf+ (These last few plus signs should not affect the final output)
    • ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++.
    • [
    • This is a loop comment.
    • Command characters, they should be ignored <here>.
    • Including [matching square brackets].
    • Oh, and "+" and >-< signs.
    • And gibberish: skldfjsdklfjs,..sd,dsf,s.fd,++--sdfsdsdfKLSDJFKLDSJ[[[][]][][[[][]]]]
    • And unmatched round brackets: ((((()))(((()))))))))))))))))))))))
    • ]
    • sdklfjsdkldfjskldfs+++++++++++++++++sdlkfjsdlkfjsdfklsdjfkldsjfd++++++++++++++++++SDLKFJSDKLJSDFKLDSFJFDSKLJDFS++++++++++++++++++++++++SDLKFJDSFKLJFSDKLDSFJKSLDFJDSFKLDFSJ+++++++++++++.>++++(+++++(+()++)+((++)(++)))++++++++++++++NESWNEWSNEWWWSEEEEE+++++++++++++++;;++++++++++++;;;;;++++++++++++;;;;+++++++++;;;;;++++++++++++********++++++++++.>+++*****++++++++++++++***;;;*****++++++++++++;;**;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++.+++++sdfsfdsdfsdfsfd++++sdfsdffdsfds++sdfsdffsddfssdf+ (These last few plus signs should not affect the final output)

After doing:

  • Extract method
  • Move method donde calcular el precio (movie or rental)?
  • Unit test smell: Indirect Testing
  • Replace temp with query
  • Replace Type Code with State/Strategy
  • Replace Conditional with Polymorphism
  • Replace Constructor with Factory Method
Code
Diff
  • class RentalReport {
      statement(customer) {
        let result = "Rental Record for " + customer.name + "\n";
        customer.rentals.forEach((each) => {
          //show figures for this rental
          result += "\t" + each.movie.title+ "\t" + each.getAmount() + "\n";
        });
        //add footer lines
        result += "Amount owed is " + customer.getTotalAmount() + "\n";
        result += "You earned " + customer.getFrequentPoints() + " frequent renter points";
        return result;
      }
    }
    
    const MovieType = {
        CHILDREN: Symbol('CHILDREN'),
        REGULAR: Symbol('REGULAR'),
        NEW_RELEASE: Symbol('NEW_RELEASE'),
    }
    
    class Price {
      static create(priceCode) {
        switch (priceCode) {
          case MovieType.REGULAR:
            return new RegularPrice();
          case MovieType.NEW_RELEASE:
            return new NewReleasePrice();
          case MovieType.CHILDREN:
            return new ChildrenPrice();
        }
      }
      get code() {
        throw "not implemented";
      }
      getAmount(daysRented) {
        throw "not implemented";
      }
      getFrequentPoints(daysRented) {
        return 1;
      }
    }
    
    class ChildrenPrice extends Price {
      get code() {
        return MovieType.CHILDREN;
      }
      getAmount(daysRented) {
        let amount = 1.5;
        if (daysRented > 3)
          amount += (daysRented - 3) * 1.5;
        return amount;  
      }
    }
    
    class RegularPrice extends Price {
      get code() {
        return MovieType.REGULAR;
      }
      getAmount(daysRented) {
        let amount = 2;
        if (daysRented > 2)
          amount += (daysRented - 2) * 1.5;
        return amount;  
      }
    }
    
    class NewReleasePrice extends Price {
      get code() {
        return MovieType.NEW_RELEASE;
      }
      getAmount(daysRented) {
        return daysRented * 3;
      }
      getFrequentPoints(daysRented) {
        return daysRented > 1 ? 2 : 1;
      }
    }
    
    
    class Movie {
      constructor (title, priceCode) {
        this._title = title;
        this.priceCode = priceCode;
      }
      set priceCode(priceCode) {
        this._price = Price.create(priceCode);
      }
      get priceCode() {
        return this._price.code;
      }
      get title() {
        return this._title;
      }
      getAmount(daysRented) {
        return this._price.getAmount(daysRented);  
      }
      getFrequentPoints(daysRented) {
        return this._price.getFrequentPoints(daysRented);
      }
    }
    
    class Rental {
      constructor(movie, daysRented) {
        this._movie = movie;
        this._daysRented = daysRented;
      }
      get daysRented() {
        return this._daysRented;
      }
      get movie() {
        return this._movie;
      }
      getAmount() {
        return this.movie.getAmount(this.daysRented);
      }
      getFrequentPoints() {
        return this.movie.getFrequentPoints(this.daysRented);
      }
    }
    
    class Customer {
      constructor(name) {
        this._name = name;
        this._rentals = [];
      }
      get name() {
        return this._name;
      }
      get rentals() {
        return this._rentals;
      }
      addRental(rental) {
        this._rentals.push(rental);
      }
      getFrequentPoints() {
        return this.rentals.reduce((frequentPoints, rental) => frequentPoints + rental.getFrequentPoints(), 0);
      }
      getTotalAmount() {
        return this.rentals.reduce((amount, rental) => amount + rental.getAmount(), 0);
      }
    }
    • class RentalReport {
    • statement(customer) {
    • let totalAmount = 0;
    • let frequentRenterPoints = 0;
    • let result = "Rental Record for " + customer.name + "\n";
    • customer.rentals.forEach((each) => {
    • let thisAmount = 0;
    • //determine amounts for each line
    • switch (each.movie.priceCode) {
    • case MovieType.REGULAR:
    • thisAmount += 2;
    • if (each.daysRented > 2)
    • thisAmount += (each.daysRented - 2) * 1.5;
    • break;
    • case MovieType.NEW_RELEASE:
    • thisAmount += each.daysRented * 3;
    • break;
    • case MovieType.CHILDREN:
    • thisAmount += 1.5;
    • if (each.daysRented > 3)
    • thisAmount += (each.daysRented - 3) * 1.5;
    • break;
    • }
    • // add frequent renter points
    • frequentRenterPoints ++;
    • // add bonus for a two day new release rental
    • if ((each.movie.priceCode == MovieType.NEW_RELEASE) && each.daysRented > 1) frequentRenterPoints ++;
    • //show figures for this rental
    • result += "\t" + each.movie.title+ "\t" + thisAmount + "
    • ";
    • totalAmount += thisAmount;
    • result += "\t" + each.movie.title+ "\t" + each.getAmount() + "
    • ";
    • });
    • //add footer lines
    • result += "Amount owed is " + totalAmount + "
    • ";
    • result += "You earned " + frequentRenterPoints + " frequent renter points";
    • result += "Amount owed is " + customer.getTotalAmount() + "
    • ";
    • result += "You earned " + customer.getFrequentPoints() + " frequent renter points";
    • return result;
    • }
    • }
    • const MovieType = {
    • CHILDREN: Symbol('CHILDREN'),
    • REGULAR: Symbol('REGULAR'),
    • NEW_RELEASE: Symbol('NEW_RELEASE'),
    • }
    • class Price {
    • static create(priceCode) {
    • switch (priceCode) {
    • case MovieType.REGULAR:
    • return new RegularPrice();
    • case MovieType.NEW_RELEASE:
    • return new NewReleasePrice();
    • case MovieType.CHILDREN:
    • return new ChildrenPrice();
    • }
    • }
    • get code() {
    • throw "not implemented";
    • }
    • getAmount(daysRented) {
    • throw "not implemented";
    • }
    • getFrequentPoints(daysRented) {
    • return 1;
    • }
    • }
    • class ChildrenPrice extends Price {
    • get code() {
    • return MovieType.CHILDREN;
    • }
    • getAmount(daysRented) {
    • let amount = 1.5;
    • if (daysRented > 3)
    • amount += (daysRented - 3) * 1.5;
    • return amount;
    • }
    • }
    • class RegularPrice extends Price {
    • get code() {
    • return MovieType.REGULAR;
    • }
    • getAmount(daysRented) {
    • let amount = 2;
    • if (daysRented > 2)
    • amount += (daysRented - 2) * 1.5;
    • return amount;
    • }
    • }
    • class NewReleasePrice extends Price {
    • get code() {
    • return MovieType.NEW_RELEASE;
    • }
    • getAmount(daysRented) {
    • return daysRented * 3;
    • }
    • getFrequentPoints(daysRented) {
    • return daysRented > 1 ? 2 : 1;
    • }
    • }
    • class Movie {
    • constructor (title, priceCode) {
    • this._title = title;
    • this._priceCode = priceCode;
    • this.priceCode = priceCode;
    • }
    • set priceCode(priceCode) {
    • this._price = Price.create(priceCode);
    • }
    • get priceCode() {
    • return this._priceCode;
    • return this._price.code;
    • }
    • get title() {
    • return this._title;
    • }
    • getAmount(daysRented) {
    • return this._price.getAmount(daysRented);
    • }
    • getFrequentPoints(daysRented) {
    • return this._price.getFrequentPoints(daysRented);
    • }
    • }
    • class Rental {
    • constructor(movie, daysRented) {
    • this._movie = movie;
    • this._daysRented = daysRented;
    • }
    • get daysRented() {
    • return this._daysRented;
    • }
    • get movie() {
    • return this._movie;
    • }
    • getAmount() {
    • return this.movie.getAmount(this.daysRented);
    • }
    • getFrequentPoints() {
    • return this.movie.getFrequentPoints(this.daysRented);
    • }
    • }
    • class Customer {
    • constructor(name) {
    • this._name = name;
    • this._rentals = [];
    • }
    • get name() {
    • return this._name;
    • }
    • get rentals() {
    • return this._rentals;
    • }
    • addRental(rental) {
    • this._rentals.push(rental);
    • }
    • }
    • getFrequentPoints() {
    • return this.rentals.reduce((frequentPoints, rental) => frequentPoints + rental.getFrequentPoints(), 0);
    • }
    • getTotalAmount() {
    • return this.rentals.reduce((amount, rental) => amount + rental.getAmount(), 0);
    • }
    • }
Code
Diff
  • function nextGeneration(grid) {
      return grid.map(() => 0);
    }
    • function nextGeneration(grid) {
    • return grid;
    • return grid.map(() => 0);
    • }
Code
Diff
  • function sayHello(input) {
      return !input ? "Hello World!" : "Hello " + input + "!"
    };
    
    • function sayHello(input) {
    • if (input !== true){
    • return "Hello World!";
    • } else {
    • return "Hello " + input + "!";
    • }
    • return !input ? "Hello World!" : "Hello " + input + "!"
    • };
Hashes
Data Structures
Code
Diff
  • /* HashMap Example */
     
    import java.util.*;
    import java.util.function.*;
    import java.util.stream.*;
     
    class HashMapDemo {
      public static void main (final String[] args) throws Exception {
        System.out.println(toString(mapFromRangeClosed(1, 6, i -> 1), formattingEntry(), "\n"));
      }
      
      public static <T> Map<Integer, T> mapFromRangeClosed(final int lo, final int hi, final Function<Integer, T> valueMapper) {
        return IntStream.rangeClosed(lo, hi).boxed().collect(Collectors.toMap(Function.identity(), valueMapper));
      }
      
      public static <K,V> String toString(final Map<K, V> map, final Function<Map.Entry<K, V>, String> entryFormatter, final CharSequence delimiter) {
        return map.entrySet().stream().map(entryFormatter).collect(Collectors.joining(delimiter));
      }
      
      public static <K,V> Function<Map.Entry<K, V>, String> formattingEntry() {
        return e -> e.getKey() + " " + e.getValue();
      }
    }
    • /* HashMap Example */
    • import java.util.*;
    • import java.lang.*;
    • import java.io.*;
    • import java.util.function.*;
    • import java.util.stream.*;
    • class HashMapDemo
    • {
    • public static void main (String[] args) throws java.lang.Exception {
    • Map<Integer, Integer> map = new HashMap<Integer, Integer>() {{
    • put(1, 1);
    • put(2, 1);
    • put(3, 1);
    • put(4, 1);
    • put(5, 1);
    • put(6, 1);
    • }};
    • map.entrySet().stream().forEach(e -> System.out.println(e.getKey() + " " + e.getValue()));
    • class HashMapDemo {
    • public static void main (final String[] args) throws Exception {
    • System.out.println(toString(mapFromRangeClosed(1, 6, i -> 1), formattingEntry(), "\n"));
    • }
    • public static <T> Map<Integer, T> mapFromRangeClosed(final int lo, final int hi, final Function<Integer, T> valueMapper) {
    • return IntStream.rangeClosed(lo, hi).boxed().collect(Collectors.toMap(Function.identity(), valueMapper));
    • }
    • public static <K,V> String toString(final Map<K, V> map, final Function<Map.Entry<K, V>, String> entryFormatter, final CharSequence delimiter) {
    • return map.entrySet().stream().map(entryFormatter).collect(Collectors.joining(delimiter));
    • }
    • public static <K,V> Function<Map.Entry<K, V>, String> formattingEntry() {
    • return e -> e.getKey() + " " + e.getValue();
    • }
    • }
Code
Diff
  • import java.util.Map;
    import java.util.function.Function;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    /**
      Time Complexity  : O(N)
      Space Complexity : O(N)
    */
    class MaxOccurence {
    
      public static int findMax(final int[] nums) {
        return IntStream.of(nums).boxed()
          .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
          .entrySet().stream()
          .max(Map.Entry.comparingByValue())
          .map(Map.Entry::getKey).orElse(-1);
      }
    }
    • import java.util.*;
    • import java.util.List;
    • import java.util.Map;
    • import java.util.Optional;
    • import java.util.function.Function;
    • import java.util.stream.Collectors;
    • import java.util.stream.IntStream;
    • /**
    • Time Complexity : O(N)
    • Space Complexity : O(N)
    • */
    • class MaxOccurence {
    • public static int findMax(int[] nums) {
    • Optional<Map.Entry<Integer, List<Integer>>> max = IntStream.of(nums)
    • .boxed()
    • .collect(Collectors.groupingBy(num -> num))
    • .entrySet()
    • .stream()
    • .max((e1, e2) -> e1.getValue().size() - e2.getValue().size());
    • return max.isPresent() ? max.get().getKey() : -1;
    • public static int findMax(final int[] nums) {
    • return IntStream.of(nums).boxed()
    • .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
    • .entrySet().stream()
    • .max(Map.Entry.comparingByValue())
    • .map(Map.Entry::getKey).orElse(-1);
    • }
    • }
Code
Diff
  • public class Primes {
      public static boolean isAPrime(int number) {
        if (number < 2 || (number % 2 == 0 && number != 2)) return false;
        for (int i = 3; i*i <= number; i += 2) {
          if (number % i == 0) return false;
        }
        return true;
      }
    }
    • public class Primes {
    • public static boolean isAPrime(int number) {
    • if(number > 1 && number == 2) return true; //1 is not a prime number by definition
    • else {
    • for(int i = 3; i*i < number; i +=2) {
    • if (number % i == 0) return false;
    • }
    • if (number < 2 || (number % 2 == 0 && number != 2)) return false;
    • for (int i = 3; i*i <= number; i += 2) {
    • if (number % i == 0) return false;
    • }
    • return true;
    • }
    • }