aboutsummaryrefslogtreecommitdiff
path: root/challenge-146
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-06 20:04:01 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-06 20:04:01 +0000
commitb900311560a2aaccd86c911d362838090ca1e2d9 (patch)
treef34065301cfed30d78cf0dadd0d2a91e8b6c54c5 /challenge-146
parent9a5bd54ed0182844ab128ddb85fb946818eb5607 (diff)
downloadperlweeklychallenge-club-b900311560a2aaccd86c911d362838090ca1e2d9.tar.gz
perlweeklychallenge-club-b900311560a2aaccd86c911d362838090ca1e2d9.tar.bz2
perlweeklychallenge-club-b900311560a2aaccd86c911d362838090ca1e2d9.zip
- Added Python/Java solutions to the task "10001st Prime Number" of week 146.
Diffstat (limited to 'challenge-146')
-rw-r--r--challenge-146/mohammad-anwar/java/theweeklychallenge/FindPrime.java55
-rw-r--r--challenge-146/mohammad-anwar/python/ch-1.py50
2 files changed, 105 insertions, 0 deletions
diff --git a/challenge-146/mohammad-anwar/java/theweeklychallenge/FindPrime.java b/challenge-146/mohammad-anwar/java/theweeklychallenge/FindPrime.java
new file mode 100644
index 0000000000..1f2b6ed0d2
--- /dev/null
+++ b/challenge-146/mohammad-anwar/java/theweeklychallenge/FindPrime.java
@@ -0,0 +1,55 @@
+package theweeklychallenge;
+
+/*
+
+Week 146:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-146
+
+Task #1: 10001st Prime Number
+
+ Write a script to generate the 10001st prime number.
+
+*/
+
+import java.lang.Math;
+import junit.framework.TestCase;
+import static junit.framework.Assert.*;
+
+public class FindPrime extends TestCase {
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(theweeklychallenge.FindPrime.class);
+ }
+
+ public void testFindPrime() {
+ assertEquals(104743, findPrime(10001));
+ }
+
+ public static int findPrime(int count) {
+
+ int c = 0;
+ int n = 2;
+ while (c <= count) {
+ if (isPrime(n)) {
+ if (++c == count) {
+ return n;
+ }
+ }
+ n++;
+ }
+
+ return 0;
+ }
+
+ public static boolean isPrime(int n) {
+
+ for(int i=2; i <= Math.sqrt(n); i++) {
+ if ((n % i) == 0) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
diff --git a/challenge-146/mohammad-anwar/python/ch-1.py b/challenge-146/mohammad-anwar/python/ch-1.py
new file mode 100644
index 0000000000..f41fdf9605
--- /dev/null
+++ b/challenge-146/mohammad-anwar/python/ch-1.py
@@ -0,0 +1,50 @@
+#!/usr/bin/python3
+
+'''
+
+Week 146:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-146
+
+Task #1: 10001st Prime Number
+
+ Write a script to generate the 10001st prime number.
+
+'''
+
+import math
+import unittest
+
+def is_prime(n):
+ i = 2
+ while (i <= int(math.sqrt(n))):
+ if ((n % i) == 0):
+ return 0
+ i += 1
+
+ return 1
+
+def find_prime(count):
+ c = 0
+ n = 2
+ while (c <= count):
+ if (is_prime(n) == 1):
+ c += 1
+ if (c == count):
+ return n
+ n += 1
+
+#
+#
+# Unit test class
+
+class TestFindPrimeNumber(unittest.TestCase):
+
+ def test_example(self):
+ self.assertEqual(
+ find_prime(10001),
+ 104743,
+ 'Example'
+ )
+
+unittest.main()