diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-05-15 11:46:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-15 11:46:27 +0100 |
| commit | 7580a31a9033eaa587f576d56e32934c1245319d (patch) | |
| tree | 1ba27471b684255359ec456d060ca7088afc84b1 /challenge-164 | |
| parent | 9530676357b10587f9f21c045bc6985db63b6d7a (diff) | |
| parent | 88c9a78a58a7e5ad4c175a8e6d6fb94283453d38 (diff) | |
| download | perlweeklychallenge-club-7580a31a9033eaa587f576d56e32934c1245319d.tar.gz perlweeklychallenge-club-7580a31a9033eaa587f576d56e32934c1245319d.tar.bz2 perlweeklychallenge-club-7580a31a9033eaa587f576d56e32934c1245319d.zip | |
Merge pull request #6106 from deadmarshal/challenge164
added challenge164 solutions.
Diffstat (limited to 'challenge-164')
| -rw-r--r-- | challenge-164/deadmarshal/cpp/ch-1.cpp | 30 | ||||
| -rw-r--r-- | challenge-164/deadmarshal/cpp/ch-2.cpp | 46 | ||||
| -rw-r--r-- | challenge-164/deadmarshal/lua/ch-1.lua | 24 | ||||
| -rw-r--r-- | challenge-164/deadmarshal/lua/ch-2.lua | 30 | ||||
| -rw-r--r-- | challenge-164/deadmarshal/pascal/ch1.pas | 31 | ||||
| -rw-r--r-- | challenge-164/deadmarshal/pascal/ch2.pas | 64 | ||||
| -rw-r--r-- | challenge-164/deadmarshal/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-164/deadmarshal/perl/ch-2.pl | 28 |
8 files changed, 270 insertions, 0 deletions
diff --git a/challenge-164/deadmarshal/cpp/ch-1.cpp b/challenge-164/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..c30efe2d02 --- /dev/null +++ b/challenge-164/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,30 @@ +#include<iostream> +#include<cmath> + +int reverse_num(int n) +{ + int rev{}, rem; + while(n) + { + rem = n % 10; + rev = rem + (rev * 10); + n /= 10; + } + return rev; +} + +bool is_prime(int n) +{ + if(n <= 1) return false; + for(int i{2}; i <= (int)sqrt(n); ++i) + if(n % i == 0) return false; + return true; +} + +int main() +{ + for(int i{}; i < 1000; ++i) + if((i == reverse_num(i)) && (is_prime(i))) + std::cout << i << ' '; + return 0; +} diff --git a/challenge-164/deadmarshal/cpp/ch-2.cpp b/challenge-164/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..c2ecc53b88 --- /dev/null +++ b/challenge-164/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,46 @@ +#include<iostream> +#include<unordered_map> +#include<cmath> + +int sum_squares(int n) +{ + int sum{}; + while(n) + { + sum += (int)std::pow(n % 10, 2); + n /= 10; + } + return sum; +} + +bool is_happy(int n) +{ + std::unordered_map<int, int> map{}; + while(1) + { + map[n]++; + n = sum_squares(n); + if(n == 1) return 1; + if(map[n] != 0) return 0; + } +} + +void happy_numbers() +{ + int i{}, count{}; + while(count < 8) + { + if(is_happy(i)) + { + std::cout << i << ' '; + count++; + } + i++; + } +} + +int main() +{ + happy_numbers(); + return 0; +} diff --git a/challenge-164/deadmarshal/lua/ch-1.lua b/challenge-164/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..ebb638c34e --- /dev/null +++ b/challenge-164/deadmarshal/lua/ch-1.lua @@ -0,0 +1,24 @@ +function is_prime(n) + assert(type(n) == 'number', 'n must be a number!') + if n <= 1 then return false end + for i=2,math.sqrt(n) do + if n % i == 0 then return false end + end + return true +end + +function reverse(n) + assert(type(n) == 'number', 'n must be a number!') + local rev = 0 + while n ~= 0 do + rev = (rev * 10) + (n % 10) + n = n // 10 + end + return rev +end + +for i=1, 1000 do + if i == reverse(i) and is_prime(i) then + io.write(i, ' ') + end +end diff --git a/challenge-164/deadmarshal/lua/ch-2.lua b/challenge-164/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..1351e892e4 --- /dev/null +++ b/challenge-164/deadmarshal/lua/ch-2.lua @@ -0,0 +1,30 @@ +function sum_squares(n) + assert(type(n) == 'number', 'n must be a number!') + local sum = 0 + while n ~= 0 do + sum = sum + (n % 10)^2 + n = n // 10 + end + return sum +end + +function is_happy(n) + assert(type(n) == 'number', 'n must be a number!') + local t = {} + while true do + t[n] = 1 + n = sum_squares(n) + if n == 1 then return true end + if t[n] ~= nil then return false end + end +end + +function happy_numbers() + local i,count = 0,0 + while count < 8 do + if is_happy(i) then io.write(i, ' ') count = count + 1 end + i = i + 1 + end +end + +happy_numbers(); diff --git a/challenge-164/deadmarshal/pascal/ch1.pas b/challenge-164/deadmarshal/pascal/ch1.pas new file mode 100644 index 0000000000..b9cd218c25 --- /dev/null +++ b/challenge-164/deadmarshal/pascal/ch1.pas @@ -0,0 +1,31 @@ +program Ch1; + +{$mode objfpc} +var + I:Integer; + +function IsPrime(N:Integer):Boolean; +var + I:Integer; +begin + if(N <= 1) then Exit(False); + for I := 2 to Trunc(Sqrt(N)) do + if(N mod I = 0) then Exit(False); + Result := True; +end; + +function ReverseNum(N:Integer):Integer; +begin + Result := 0; + while(N <> 0) do + begin + Result := (Result * 10) + (N mod 10); + N := N div 10; + end; +end; + +begin + for I := 1 to 1000 do + if((I = ReverseNum(I)) and IsPrime(I)) then + Write(I, ' '); +end. diff --git a/challenge-164/deadmarshal/pascal/ch2.pas b/challenge-164/deadmarshal/pascal/ch2.pas new file mode 100644 index 0000000000..90a7250e82 --- /dev/null +++ b/challenge-164/deadmarshal/pascal/ch2.pas @@ -0,0 +1,64 @@ +program Ch2; + +{$mode objfpc} + +uses + SysUtils,Math,Generics.Collections; + +function SumSquares(N:Integer):Integer; +begin + Result := 0; + while N <> 0 do + begin + Result := Result + Trunc(Power((N mod 10),2)); + n := N div 10; + end; +end; + +function IsHappy(N:Integer):Boolean; +type + TMap = specialize THashMap<Integer,Integer>; +var + Value:Integer; + Map:TMap; +begin + Map := TMap.Create; + while True do + begin + Map.Add(N, 1); + N := SumSquares(N); + if(N = 1) then + begin + Result := True; + break; + end; + Map.TryGetValue(N,Value); + if(Value <> 0) then + begin + Result := False; + break; + end; + end; + FreeAndNil(Map); +end; + +procedure HappyNumbers; +var + I,Count:Integer; +begin + I := 0; + Count := 0; + while Count < 8 do + begin + if IsHappy(I) then + begin + Write(I, ' '); + Inc(Count); + end; + Inc(I); + end; +end; + +begin + HappyNumbers; +end. diff --git a/challenge-164/deadmarshal/perl/ch-1.pl b/challenge-164/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..3a0a269003 --- /dev/null +++ b/challenge-164/deadmarshal/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +sub is_prime{ + my ($n) = @_; + if($n <= 1){return 0;} + foreach(2..sqrt($n)){ + return 0 if $n % $_ == 0; + } + return 1; +} + +foreach(1..1000){ + print "$_ " if ($_ == reverse $_) && (is_prime($_)); +} + diff --git a/challenge-164/deadmarshal/perl/ch-2.pl b/challenge-164/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..42bc0c90df --- /dev/null +++ b/challenge-164/deadmarshal/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(sum0); + +sub is_happy{ + my ($n) = @_; + my %seen; + while(1){ + $n = sum0(map {$_ ** 2} split //, $n); + return 1 if $n == 1; + return 0 if $seen{$n}++; + } +} + +sub happy_numbers{ + my $i = 0; + my $count = 0; + while($count < 8){ + if(is_happy($i)){ + print "$i "; + $count++; + } + $i++; + } +} + +happy_numbers; |
