diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-01-17 09:00:14 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-01-17 09:00:14 +0000 |
| commit | 990d5177887cda20c8a4804b91afe5e9bcaaf201 (patch) | |
| tree | 8c2ae1ac10ff43e872f3cab2611255c76841f4c3 /challenge-147/mohammad-anwar | |
| parent | dd99d53e09a332c4ff5153b8bb54c9506a03860d (diff) | |
| parent | ee9f8c8288170dc442f8199fc1d2f7e45ac4377c (diff) | |
| download | perlweeklychallenge-club-990d5177887cda20c8a4804b91afe5e9bcaaf201.tar.gz perlweeklychallenge-club-990d5177887cda20c8a4804b91afe5e9bcaaf201.tar.bz2 perlweeklychallenge-club-990d5177887cda20c8a4804b91afe5e9bcaaf201.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-147/mohammad-anwar')
3 files changed, 276 insertions, 0 deletions
diff --git a/challenge-147/mohammad-anwar/java/theweeklychallenge/TruncatablePrime.java b/challenge-147/mohammad-anwar/java/theweeklychallenge/TruncatablePrime.java new file mode 100644 index 0000000000..9aa5ed41cb --- /dev/null +++ b/challenge-147/mohammad-anwar/java/theweeklychallenge/TruncatablePrime.java @@ -0,0 +1,90 @@ +package theweeklychallenge; + +/* + +Week 147: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-147 + +Task #1: Truncatable Prime + + Write a script to generate first 20 left-truncatable prime numbers in base 10. + +*/ + +import java.lang.Math; +import java.util.Arrays; +import java.util.ArrayList; +import junit.framework.TestCase; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import static junit.framework.Assert.*; + +public class TruncatablePrime extends TestCase { + + public static void main(String[] args) { + junit.textui.TestRunner.run(theweeklychallenge.TruncatablePrime.class); + } + + public void testTruncatablePrime() { + Integer exp[] = {2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197}; + Integer got[] = leftTruncatablePrime(20); + assertTrue(Arrays.equals(exp, got)); + } + + public static Integer[] leftTruncatablePrime(int count) { + ArrayList<Integer> ltp = new ArrayList<Integer>(); + int c = 0; + int n = 2; + while (c < count) { + String s = Integer.toString(n); + + if (!Pattern.compile(".*0.*").matcher(s).matches() && isPrime(n)) { + Integer[] numbers = leftTruncatableNumbers(n); + boolean found = true; + if (numbers.length >=2) { + for(int i = 0; i<numbers.length; i++) { + if (!isPrime(numbers[i])) { + found = false; + } + } + } + if (found) { + ltp.add(n); + c++; + } + } + n++; + } + + Integer[] ltpArray = new Integer[ltp.size()]; + return ltp.toArray(ltpArray); + } + + public static Integer[] leftTruncatableNumbers(int n) { + int i = 0; + String s = Integer.toString(n); + ArrayList<Integer> numbers = new ArrayList<Integer>(); + while (i < s.length()) { + numbers.add(Integer.valueOf(s.substring(i))); + i++; + } + + Integer[] numArray = new Integer[numbers.size()]; + return numbers.toArray(numArray); + } + + public static boolean isPrime(int n) { + if (n == 1) { + return false; + } + + for(int i=2; i <= Math.sqrt(n); i++) { + if ((n % i) == 0) { + return false; + } + } + + return true; + } +} diff --git a/challenge-147/mohammad-anwar/python/ch-1.py b/challenge-147/mohammad-anwar/python/ch-1.py new file mode 100644 index 0000000000..57529cc76e --- /dev/null +++ b/challenge-147/mohammad-anwar/python/ch-1.py @@ -0,0 +1,75 @@ +#!/usr/bin/python3 + +''' + +Week 147: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-147 + +Task #1: Truncatable Primme + + Write a script to generate first 20 left-truncatable prime numbers in base 10. + +''' + +import re +import math +import numpy +import unittest + +def is_prime(n): + if (n == 1): + return False + + i = 2 + while (i <= int(math.sqrt(n))): + if ((n % i) == 0): + return False + i += 1 + + return True + +def left_truncatable_numbers(n): + numbers = [] + i = 0 + s = str(n) + while (i < len(s)): + numbers.append(int(s[i:])) + i += 1 + + return numbers + +def left_truncatable_primes(count): + ltp = [] + c = 0 + n = 2 + while (c < count): + containZero = re.search('0', str(n)) + if ((not containZero) and is_prime(n)): + numbers = left_truncatable_numbers(n) + found = True + if (len(numbers) >= 2): + for i in numbers: + if (not is_prime(i)): + found = False + + if (found): + ltp.append(n) + c += 1 + + n += 1 + + return ltp + +# +# +# Unit test class + +class TestTruncatablePrime(unittest.TestCase): + + def test_example(self): + exp = [2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197] + got = left_truncatable_primes(20) + self.assertEqual(numpy.alltrue(exp == got), True, 'Example') + +unittest.main() diff --git a/challenge-147/mohammad-anwar/swift/ch-1.swift b/challenge-147/mohammad-anwar/swift/ch-1.swift new file mode 100644 index 0000000000..933c5cfd56 --- /dev/null +++ b/challenge-147/mohammad-anwar/swift/ch-1.swift @@ -0,0 +1,111 @@ +import Foundation + +/* + +Week 147: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-147 + +Task #1: Truncatable Prime + + Write a script to generate first 20 left-truncatable prime numbers in base 10. + +*/ + +enum ParamError: Error { + case missingCount + case invalidCount +} + +do { + let paramCount:Int = Int(CommandLine.argc) + + if paramCount <= 1 { + throw ParamError.missingCount + } + + let count:Int = Int(CommandLine.arguments[1])! + if count > 0 { + var c:Int = 0 + var n:Int = 2 + while c < count { + if !containZero(String(n)) && isPrime(n) { + let numbers:[Int] = leftTruncatableNumbers(n) + var found:Bool = true + if numbers.count >= 2 { + for _n in numbers { + if !isPrime(_n) { + found = false + } + } + } + + if found { + print(n) + c += 1 + } + } + + n += 1 + } + } + else { + throw ParamError.invalidCount + } +} +catch ParamError.missingCount { + print("Missing index count.") +} +catch ParamError.invalidCount { + print("Invalid index count.") +} +catch let error { + print(error) +} + +// +// +// Functions + +func containZero(_ n:String) -> Bool { + + let pattern = "0" + let regex = try! NSRegularExpression(pattern: pattern) + let range = NSRange(location: 0, length: n.utf16.count) + + if regex.firstMatch(in: n, options: [], range: range) != nil { + return true + } + else { + return false + } +} + +func leftTruncatableNumbers(_ n:Int) -> Array<Int> { + var numbers = [Int]() + var i:Int = 0 + let s:String = String(n) + let l:Int = s.count - 1 + while i < s.count { + numbers.append(Int(String(Array(s)[i...l]))!) + i += 1 + } + + return numbers; +} + +func isPrime(_ n:Int) -> Bool { + if n == 1 { + return false + } + + let j:Int = Int(sqrt(Float(n))) + if j >= 2 { + for i in 2...j { + if n % i == 0 { + return false + } + } + } + return true +} |
