diff options
Diffstat (limited to 'challenge-146/khalid-anwar')
| -rw-r--r-- | challenge-146/khalid-anwar/php/ch-2.php | 55 | ||||
| -rw-r--r-- | challenge-146/khalid-anwar/python/ch-1.py | 46 |
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-146/khalid-anwar/php/ch-2.php b/challenge-146/khalid-anwar/php/ch-2.php new file mode 100644 index 0000000000..a63a3a5046 --- /dev/null +++ b/challenge-146/khalid-anwar/php/ch-2.php @@ -0,0 +1,55 @@ +<?php +////////////////////////////////////////////////////// +// Fraction Tree Solution : Designed by Khalid Anwar// +// 1/1 // +// / \ // +// 1/2 2/1 // +// / \ / \ // +// 1/3 3/2 2/3 3/1 // +// / \ / \ / \ / \ // +// 1/4 4/3 3/5 5/2 2/5 5/3 3/4 4/1 // +// ************************************************ // +// Please note, Its working code but Not fully // +// optimized , can be checked manually by providing // +// member detail from below itself, please refer // +// diagram above for Fraction Tree. // +////////////////////////////////////////////////////// + +function getNumeratorDenominator($fraction) +{ + preg_match('/^(?P<whole>\d+)?\s?((?P<numerator>\d+)\/(?P<denominator>\d+))?$/', $fraction, $components); + // Extract numerator, and denominator components + $numerator = $components['numerator'] ?: 0; + $denominator = $components['denominator'] ?: 0; + return $components; +} + +function getParent($c, $d){ + $parent = Array(); + if ($c > $d){ + $parent['p_nominator'] = $c-$d; + $parent['p_denominator'] = $d; + }else{ + $parent['p_nominator'] = $c; + $parent['p_denominator'] = $d-$c; + } + return $parent; +} + +// Testing of member input, first input is set as default in this code. Others can be tested by uncommenting it accordingly +// online php editor reference : https://paiza.io/en/projects/new + +$member = '3/5'; //input 1 +//$member = '4/3'; //input 2 +//$member = '3/4'; //input 3 + +$var = getNumeratorDenominator($member); + +echo "For member =".$member."\n"; +$parent = getParent($var['numerator'],$var['denominator']); +echo "Parent = ".$parent['p_nominator']."/".$parent['p_denominator']; +echo " and "; +$grandParent = getParent($parent['p_nominator'],$parent['p_denominator']); +echo "GrandParent = ".$grandParent['p_nominator']."/".$grandParent['p_denominator']; + +?> diff --git a/challenge-146/khalid-anwar/python/ch-1.py b/challenge-146/khalid-anwar/python/ch-1.py new file mode 100644 index 0000000000..62e628bbdd --- /dev/null +++ b/challenge-146/khalid-anwar/python/ch-1.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +""" Returns the nth prime number By Khalid Anwar +""" +def get_nth_prime_number(nth): + total_number_of_primes = 0 + factor = 2 + s = (nth * factor) + while total_number_of_primes < nth: + primes = make_prime(s) + total_number_of_primes = sum(primes[2:]) + factor += 1 + s = (nth * factor) + nth_prime_number = count_primes(primes, nth) + return nth_prime_number + +""" using the Sieve of Eratosthenes +""" +def make_prime(k): + prime = bytearray([1]*k) + for i in range(2, k): + if prime[i] == 1: + for j in range(i, k): + if i*j < k: + prime[i*j] = 0 + else: + break + return prime + +""" Returns the n-th prime +""" +def count_primes(primes, nth): + count = 0 + for k in range(2, len(primes)): + count += primes[k] + if count == nth: + return k + + +def main(): + NTH = 10001 + nth_prime_number = get_nth_prime_number(NTH) + print("The {}-th prime number is => {}".format(NTH, nth_prime_number)) + + +if __name__ == "__main__": + main() |
