diff options
| author | James Smith <js5@sanger.ac.uk> | 2023-01-03 11:42:25 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-03 11:42:25 +0000 |
| commit | ca0685c7e29a38ca0d2345f949b212874abd9e68 (patch) | |
| tree | 95b89a3bdfe888aea2efed8a3b8eff3a4f071623 /challenge-198 | |
| parent | af714464d403226d86b6a8dec53582014daaab64 (diff) | |
| parent | ee249218f373166edca2b95144a9b0b59e200e05 (diff) | |
| download | perlweeklychallenge-club-ca0685c7e29a38ca0d2345f949b212874abd9e68.tar.gz perlweeklychallenge-club-ca0685c7e29a38ca0d2345f949b212874abd9e68.tar.bz2 perlweeklychallenge-club-ca0685c7e29a38ca0d2345f949b212874abd9e68.zip | |
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-198')
| -rw-r--r-- | challenge-198/robert-dicicco/julia/ch-2.jl | 69 | ||||
| -rw-r--r-- | challenge-198/robert-dicicco/perl/ch-2.pl | 75 | ||||
| -rw-r--r-- | challenge-198/robert-dicicco/python/ch-2.py | 81 | ||||
| -rw-r--r-- | challenge-198/robert-dicicco/raku/ch-2.raku | 65 | ||||
| -rw-r--r-- | challenge-198/robert-dicicco/ruby/ch-2.rb | 73 | ||||
| -rw-r--r-- | challenge-198/wlmb/blog.txt | 2 | ||||
| -rwxr-xr-x | challenge-198/wlmb/perl/ch-1.pl | 14 | ||||
| -rwxr-xr-x | challenge-198/wlmb/perl/ch-2.pl | 12 | ||||
| -rw-r--r-- | challenge-198/zapwai/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-198/zapwai/perl/ch-2.pl | 4 | ||||
| -rw-r--r-- | challenge-198/zapwai/rust/ch-1.rs | 23 | ||||
| -rw-r--r-- | challenge-198/zapwai/rust/ch-2.rs | 19 | ||||
| -rw-r--r-- | challenge-198/ziameraj16/java/PrimeCount.java | 37 |
13 files changed, 509 insertions, 0 deletions
diff --git a/challenge-198/robert-dicicco/julia/ch-2.jl b/challenge-198/robert-dicicco/julia/ch-2.jl new file mode 100644 index 0000000000..01b5fcc756 --- /dev/null +++ b/challenge-198/robert-dicicco/julia/ch-2.jl @@ -0,0 +1,69 @@ +#!/usr/bin/env julia + +#= + +AUTHOR: Robert DiCicco + +DATE : 2023-01-02 + +Challenge 198 Prime Count ( Julia ) + +=# + +using Printf + +using Primes + + + +arr = [10,15,1,25] + + + +for n in arr + + cnt = 0 + + @printf("Input: \$n = %d\n",n) + + for x in 1:n + + if isprime(x) + + cnt += 1 + + end + + end + + @printf("Output: %d\n\n",cnt) + +end + +#= + +julia .\PrimeCount.jl + +Input: $n = 10 + +Output: 4 + + + +Input: $n = 15 + +Output: 6 + + + +Input: $n = 1 + +Output: 0 + + + +Input: $n = 25 + +Output: 9 + +=# diff --git a/challenge-198/robert-dicicco/perl/ch-2.pl b/challenge-198/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..d45956061a --- /dev/null +++ b/challenge-198/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,75 @@ +#!/usr/bin/env perl + +=begin + +AUTHOR: Robert DiCicco + +DATE : 2023-01-02 + +Challenge 198 Prime Count ( Perl ) + +=cut + +use strict; + +use warnings; + +use feature qw/say/; + +use ntheory qw/is_prime/; + + + +my @arr = (10,15,1,25); + + + +for my $x (0..scalar(@arr)-1) { + + my $cnt = 0; + + print "Input: \$n = $arr[$x]\n"; # find primes less than @arr[$x] + + for my $n (0..$arr[$x]){ + + if(is_prime($n)) { + + $cnt++; + + } + + } + + print "Output: $cnt\n\n"; + +} + + + +=begin + +perl .\PrimeCount.pl + +Input: $n = 10 + +Output: 4 + + + +Input: $n = 15 + +Output: 6 + + + +Input: $n = 1 + +Output: 0 + + + +Input: $n = 25 + +Output: 9 + +=cut diff --git a/challenge-198/robert-dicicco/python/ch-2.py b/challenge-198/robert-dicicco/python/ch-2.py new file mode 100644 index 0000000000..71d5718b18 --- /dev/null +++ b/challenge-198/robert-dicicco/python/ch-2.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python + +''' + +AUTHOR: Robert DiCicco + +DATE : 2023-01-02 + +Challenge 198 Prime Count ( Python ) + +''' + + + +arr = [10,15,1,25] + + + +def isprime(num): + + if num > 1: + + for n in range(2,num): + + if (num % n) == 0: + + return False + + return True + + else: + + return False + + + +for n in arr: + + cnt = 0 + + print(f"Input: $n = {n}") + + for x in range(n): + + if isprime(x): + + #rint(x) + + cnt += 1 + + print(f"Output: {cnt}\n") + + + +''' + +python .\PrimeCount.py + +Input: $n = 10 + +Output: 4 + + + +Input: $n = 15 + +Output: 6 + + + +Input: $n = 1 + +Output: 0 + + + +Input: $n = 25 + +Output: 9 + +''' diff --git a/challenge-198/robert-dicicco/raku/ch-2.raku b/challenge-198/robert-dicicco/raku/ch-2.raku new file mode 100644 index 0000000000..fdda9118f9 --- /dev/null +++ b/challenge-198/robert-dicicco/raku/ch-2.raku @@ -0,0 +1,65 @@ +#!/usr/bin/env raku + +#`{ + +AUTHOR: Robert DiCicco + +DATE : 2023-01-02 + +Challenge 198 Prime Count ( Raku ) + +} + + + +my @arr = [10,15,1,25]; + + + +for (@arr) -> $n { + + my $cnt = 0; + + print "Input: \$n = $n\n"; # find primes less than @arr[$n] + + for (0 .. $n - 1) -> $x { + + if $x.is-prime { + + $cnt++; + + } + + } + + print "Output: $cnt\n\n"; + +} + +#`{ + +raku .\PrimeCount.rk + +Input: $n = 10 + +Output: 4 + + + +Input: $n = 15 + +Output: 6 + + + +Input: $n = 1 + +Output: 0 + + + +Input: $n = 25 + +Output: 9 + +} diff --git a/challenge-198/robert-dicicco/ruby/ch-2.rb b/challenge-198/robert-dicicco/ruby/ch-2.rb new file mode 100644 index 0000000000..81b9fb45a5 --- /dev/null +++ b/challenge-198/robert-dicicco/ruby/ch-2.rb @@ -0,0 +1,73 @@ +#!/usr/bin/env ruby + +=begin + +AUTHOR: Robert DiCicco + +DATE : 2023-01-02 + +Challenge 198 Prime Count ( Ruby ) + +=end + + + +require 'prime' + +arr = [10,15,1,25] + + + +arr.each do |n| + + cnt = 0 + + puts("Input: $n = #{n}") + + for x in 0..n-1 do + + if Prime.prime?(x) #=> true + + cnt += 1 + + end + + end + + puts("Output: #{cnt}") + + puts(" ") + +end + + + +=begin + +ruby .\PrimeCount.rb + +Input: $n = 10 + +Output: 4 + + + +Input: $n = 15 + +Output: 6 + + + +Input: $n = 1 + +Output: 0 + + + +Input: $n = 25 + +Output: 9 + + + +=end diff --git a/challenge-198/wlmb/blog.txt b/challenge-198/wlmb/blog.txt new file mode 100644 index 0000000000..d193f27261 --- /dev/null +++ b/challenge-198/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/01/02/PWC198/ + diff --git a/challenge-198/wlmb/perl/ch-1.pl b/challenge-198/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..1f8b33681c --- /dev/null +++ b/challenge-198/wlmb/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +# Perl weekly challenge 198 +# Task 1: Max Gap +# +# See https://wlmb.github.io/2023/01/02/PWC198/#task-1-max-gap +use v5.36; +my @sorted=sort {$a<=>$b} @ARGV; +my %count; +my $max; +for(1..@sorted-1){ + ++$count{my $gap=$sorted[$_]-$sorted[$_-1]}; + $max=$gap if !defined $max || $gap>$max; +}; +say join " ", @ARGV, "->", $count{$max}||0 diff --git a/challenge-198/wlmb/perl/ch-2.pl b/challenge-198/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..000c86aa6a --- /dev/null +++ b/challenge-198/wlmb/perl/ch-2.pl @@ -0,0 +1,12 @@ +#!/usr/bin/env perl +# Perl weekly challenge 198 +# Task 2: Prime Count +# +# See https://wlmb.github.io/2023/01/02/PWC198/#task-2-prime-count +use v5.36; +use Math::Prime::Util qw(prime_count); +say(<<~"FIN"), exit unless @ARGV; + Usage: $0 N1 [N2...] + to fin the number of primes below N1, N2... + FIN +say "$_ -> ", prime_count($_-1) for @ARGV; diff --git a/challenge-198/zapwai/perl/ch-1.pl b/challenge-198/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..1d75a5b9ca --- /dev/null +++ b/challenge-198/zapwai/perl/ch-1.pl @@ -0,0 +1,35 @@ +my @list1 = (5,8,2,1,11,17,20,12,15); +my @list2 = (); +my @list3 = (2,5,1); +foreach my $ref (\@list1, \@list2, \@list3) { + my @list = @$ref; + print "Input: \@list = (".join(",",@list).")\n"; + @list = sort { $a <=> $b } @list; + my $gap = 0; + for (0 .. $#list - 1) { + my $diff = $list[$_ + 1] - $list[$_]; + $gap = $diff if ($gap < $diff); + } + my @index; + for (0 .. $#list - 1) { + my $diff = $list[$_ + 1] - $list[$_]; + push @index, $_ if ($diff == $gap); + } + print "Output: ", (scalar @index)."\n"; + if (scalar @index > 1) { + print "\nSince the sorted list (". + join(",",@list).") contains ".(scalar @index)." such pairs.\n"; + my @lists; + foreach (@index) { + my $str = "(".$list[$_].", ".$list[$_+1].")"; + push @lists, $str; + } + my $last = pop @lists; + my $output = join(", ", @lists)." and $last"; + print $output."\n"; + } elsif (scalar @index == 1) { + print "\n(".join(",",@list).") contains "; + print "(".$list[$index[0]].", ".$list[$index[0]+1].")\n"; + } + print "\n"; +} diff --git a/challenge-198/zapwai/perl/ch-2.pl b/challenge-198/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..b299714c65 --- /dev/null +++ b/challenge-198/zapwai/perl/ch-2.pl @@ -0,0 +1,4 @@ +my $n = $ARGV[0] || 10; +my @cnt = grep { $_ == 1 } map { 1 if &is_prime } (2 .. $n); +print "Input: \$n = $n\nOutput: ".($#cnt + 1)."\n"; +sub is_prime { for my $i (2 .. sqrt($_)) { return 0 if ($_ % $i == 0) } 1} diff --git a/challenge-198/zapwai/rust/ch-1.rs b/challenge-198/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..a60da9a555 --- /dev/null +++ b/challenge-198/zapwai/rust/ch-1.rs @@ -0,0 +1,23 @@ +fn main() { + // let mut list = [1,5,1,1,6,4]; + let mut list = vec![1,3,2,2,3,1]; + println!("Input: {:?}",list); + list.sort(); + let mut gap = 0; + for i in 1 .. list.len()-1 { + let diff = list[i+1] - list[i]; + if gap < diff { + gap = diff; + } + } + println!("The sorted list is {:?}",list); + print!("Which contains: "); + for i in 1 .. list.len()-1 { + let diff = list[i+1] - list[i]; + if diff == gap { + print!("({},{}) ",list[i],list[i+1]); + } + } + println!(""); + +} diff --git a/challenge-198/zapwai/rust/ch-2.rs b/challenge-198/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..342266433a --- /dev/null +++ b/challenge-198/zapwai/rust/ch-2.rs @@ -0,0 +1,19 @@ +use std::io; +fn main() { + println!("Please enter a value for n: "); + let mut n = String::new(); + io::stdin() .read_line(&mut n); + let num = n.trim().parse::<i32>().unwrap(); + let mut cnt = 0; + for i in 2 ..= num { + if is_prime(i) { cnt += 1 } + } + println!("Input: {num}"); + println!("Output: {cnt}"); +} +fn is_prime( n: i32 ) -> bool { + for i in 2 ..= n/2 { + if n % i == 0 { return false } + } + return true; +} diff --git a/challenge-198/ziameraj16/java/PrimeCount.java b/challenge-198/ziameraj16/java/PrimeCount.java new file mode 100644 index 0000000000..26e5d75b0f --- /dev/null +++ b/challenge-198/ziameraj16/java/PrimeCount.java @@ -0,0 +1,37 @@ +import java.util.Scanner; + +public class PrimeCount { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("Enter a number"); + final int i = Integer.parseInt(scanner.nextLine()); + int numberOfPrimes = 1; + if (i == 1) { + System.out.println(0); + } else if (i == 2) { + System.out.println(1); + } else { + for (int j = 3 ; j <= i; j++) { + if (isPrime(j)) { + numberOfPrimes++; + } + } + System.out.println(numberOfPrimes); + } + } + + private static boolean isPrime(int num) { + if (num % 2 == 0) { + return false; + } + int i = 3; + while (i < num / 2) { + if (num % i == 0) { + return false; + } + i = i + 2; + } + return true; + } +} |
