diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2022-10-29 14:00:28 +0330 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2022-10-29 14:00:28 +0330 |
| commit | 3be924cb2c725abe4f17f11af09b60c791bd32dd (patch) | |
| tree | 78433f70528af4d93621378d73c922f32614d32e | |
| parent | 6eae1d7b71ebe06f49e75b82aafdcf8dfee15431 (diff) | |
| download | perlweeklychallenge-club-3be924cb2c725abe4f17f11af09b60c791bd32dd.tar.gz perlweeklychallenge-club-3be924cb2c725abe4f17f11af09b60c791bd32dd.tar.bz2 perlweeklychallenge-club-3be924cb2c725abe4f17f11af09b60c791bd32dd.zip | |
Challenge150
| -rw-r--r-- | challenge-150/deadmarshal/ada/ch1.adb | 30 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/ada/ch2.adb | 52 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/cpp/ch-1.cpp | 25 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/cpp/ch-2.cpp | 45 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/d/ch1.d | 15 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/d/ch2.d | 35 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/lua/ch-1.lua | 17 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/lua/ch-2.lua | 38 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/nim/ch1.nim | 9 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/nim/ch2.nim | 27 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/pascal/ch1.pas | 33 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/pascal/ch2.pas | 53 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/python/ch1.py | 14 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/python/ch2.py | 16 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/raku/ch-1.raku | 14 | ||||
| -rw-r--r-- | challenge-150/deadmarshal/raku/ch-2.raku | 17 |
16 files changed, 440 insertions, 0 deletions
diff --git a/challenge-150/deadmarshal/ada/ch1.adb b/challenge-150/deadmarshal/ada/ch1.adb new file mode 100644 index 0000000000..fa944fafc7 --- /dev/null +++ b/challenge-150/deadmarshal/ada/ch1.adb @@ -0,0 +1,30 @@ +with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO; +with Ada.Command_Line; use Ada.Command_Line; +with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; +with Ada.Containers.Vectors; + +procedure Ch1 is + Not_Enough_Arguments:exception; + function TUS(Source:in String) return Unbounded_String renames To_Unbounded_String; + + function Fibonacci_Words(S1,S2:Unbounded_String) return Unbounded_String is + package String_Vec is new Ada.Containers.Vectors + (Index_Type => Natural, + Element_Type => Unbounded_String); + use String_Vec; + Vec:Vector := S1 & S2; + begin + loop + Vec.Append(Vec.Element(Vec.Last_Index-1) & Vec.Last_Element, 1); + exit when Length(Vec.Last_Element) >= 52; + end loop; + return Unbounded_Slice(Vec.Last_Element,51,51); + end Fibonacci_Words; + +begin + if Argument_Count < 2 then + raise Not_Enough_Arguments; + end if; + Put_Line(Fibonacci_Words(TUS(Argument(1)), TUS(Argument(2)))); +end Ch1; + diff --git a/challenge-150/deadmarshal/ada/ch2.adb b/challenge-150/deadmarshal/ada/ch2.adb new file mode 100644 index 0000000000..f0756ab1ea --- /dev/null +++ b/challenge-150/deadmarshal/ada/ch2.adb @@ -0,0 +1,52 @@ +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Containers.Vectors; + +procedure Ch2 is + package Natural_Vec is new Ada.Containers.Vectors + (Index_Type => Natural, + Element_Type => Natural); + use Natural_Vec; + + function Prime_Factors(Num:in Natural) return Vector is + Res:Vector; + C:Natural := 2; + N:Natural := Num; + begin + while N > 1 loop + if N mod C = 0 then Append(Res,C); N := N / c; + else C := C + 1; + end if; + end loop; + return Res; + end Prime_Factors; + + function Uniq(Vec:Vector) return Boolean is + begin + for I in Vec.First_Index..Vec.Last_Index loop + for J in I+1..Vec.Last_Index loop + if Vec.Element(I) = Vec.Element(J) then return False; end if; + end loop; + end loop; + return True; + end Uniq; + + function Square_Free_Integer return Vector is + Res,Primes:Vector; + I:Natural := 1; + begin + loop + Primes := Prime_Factors(I); + if Uniq(Primes) then Res.Append(I); end if; + I := I + 1; + exit when I >= 500; + end loop; + return Res; + end Square_Free_Integer; + + SFI:Vector := Square_Free_Integer; +begin + for E of SFI loop + Put(Natural'Image(E)); + end loop; + New_Line; +end Ch2;
\ No newline at end of file diff --git a/challenge-150/deadmarshal/cpp/ch-1.cpp b/challenge-150/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..e289ebeb3c --- /dev/null +++ b/challenge-150/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,25 @@ +#include<iostream> +#include<vector> +#include<string> +#include<cstdlib> + +int main(int argc, char *argv[]) +{ + if(argc < 2) + { + std::cerr << "Provide 2 args!"; + exit(1); + } + + std::vector<std::string> v{}; + v.push_back(argv[1]); + v.push_back(argv[2]); + + do{ + v.push_back(v.end()[-2] + v.end()[-1]); + }while(v[v.size()-1].size() <= 51); + + std::cout << v[v.size()-1][50] << "\n"; + + return 0; +} diff --git a/challenge-150/deadmarshal/cpp/ch-2.cpp b/challenge-150/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..0ea102055f --- /dev/null +++ b/challenge-150/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,45 @@ +#include<iostream> +#include<vector> +#include<algorithm> + +std::vector<int> prime_factors(int n) +{ + int c = 2; + std::vector<int> v{}; + while(n > 1) + { + if(n % c == 0) + { + v.push_back(c); + n /= c; + } + else c++; + } + return v; +} + +std::vector<int> square_free_integers() +{ + std::vector<int> v{}; + int i = 1; + do{ + std::vector<int> factors = prime_factors(i); + std::sort(factors.begin(), factors.end()); + const bool hasDups = std::adjacent_find(factors.begin(), + factors.end()) + != factors.end(); + if(!hasDups) + v.push_back(i); + i++; + }while(i <= 500); + return v; +} + +int main() +{ + std::vector<int> v = square_free_integers(); + for(const auto& e : v) + std::cout << e << " "; + std::cout << "\n"; + return 0; +} diff --git a/challenge-150/deadmarshal/d/ch1.d b/challenge-150/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..b14bf0a7cd --- /dev/null +++ b/challenge-150/deadmarshal/d/ch1.d @@ -0,0 +1,15 @@ +import std.stdio:writeln; + +char fibonacci_words(string a, string b) +{ + string[] arr; + do{ + arr ~= a ~= b; + }while(arr[$-1].length < 52); + return arr[$-1].dup[50]; +} + +void main() +{ + writeln(fibonacci_words("1234", "5678")); +} diff --git a/challenge-150/deadmarshal/d/ch2.d b/challenge-150/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..130d5b1974 --- /dev/null +++ b/challenge-150/deadmarshal/d/ch2.d @@ -0,0 +1,35 @@ +import std.stdio:write; +import std.algorithm:uniq; +import std.array:array; + +ulong[] prime_factors(ulong n) +{ + ulong c = 2; + ulong[] arr; + while(n > 1) + { + if(n % c == 0) + { + arr ~= c; + n /= c; + } + else c++; + } + return arr; +} + +void square_free_integers() +{ + ulong i = 1; + do{ + ulong[] primes = prime_factors(i); + ulong prime = primes.uniq().array.length; + if(primes.length - prime == 0) write(i, ' '); + i++; + }while(i <= 500); +} + +void main() +{ + square_free_integers(); +} diff --git a/challenge-150/deadmarshal/lua/ch-1.lua b/challenge-150/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..e6e0673329 --- /dev/null +++ b/challenge-150/deadmarshal/lua/ch-1.lua @@ -0,0 +1,17 @@ +assert(#arg == 2, "provide 2 args!") + +local function fibonacci_words(t) + assert(type(t) == "table", "t must be a table!") + repeat + t[#t+1] = t[#t-1] .. t[#t] + until #t[#t] >= 51 + local chars = {} + for i=1, #t[#t] do + local c = t[#t]:sub(i,i) + chars[#chars+1] = c + end + return chars[51] +end + +print(fibonacci_words({arg[1], arg[2]})) + diff --git a/challenge-150/deadmarshal/lua/ch-2.lua b/challenge-150/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..c76007c02e --- /dev/null +++ b/challenge-150/deadmarshal/lua/ch-2.lua @@ -0,0 +1,38 @@ +local function prime_factors(n) + assert(type(n) == "number", "n must be a number!") + local t, c = {}, 2 + while n > 1 do + if n % c == 0 then + t[#t+1] = c + n = n // c + else c = c + 1 + end + end + return t +end + +local function uniq(t) + assert(type(t) == "table", "t must be a table!") + table.sort(t) + for i=1, #t do + for j=i+1, #t do + if t[i] == t[j] then return false end + end + end + return true +end + +local function square_free_integers() + local t, i = {}, 1 + repeat + if uniq(prime_factors(i)) then + t[#t+1] = i + end + i = i + 1 + until i >= 500 + return t +end + +local sfi = square_free_integers() +for i=1, #sfi do io.write(sfi[i], ' ') end + diff --git a/challenge-150/deadmarshal/nim/ch1.nim b/challenge-150/deadmarshal/nim/ch1.nim new file mode 100644 index 0000000000..e32ec307b6 --- /dev/null +++ b/challenge-150/deadmarshal/nim/ch1.nim @@ -0,0 +1,9 @@ +import std/strutils + +proc fibonacci_words(a,b:string):int = + var s:seq[string] = @[a,b] + while s[s.high].len < 52: + s.add(s[s.high-1] & s[s.high]) + return parseInt($s[s.high][50]) + +echo fibonacci_words("1234", "5678") diff --git a/challenge-150/deadmarshal/nim/ch2.nim b/challenge-150/deadmarshal/nim/ch2.nim new file mode 100644 index 0000000000..8ee8fb7801 --- /dev/null +++ b/challenge-150/deadmarshal/nim/ch2.nim @@ -0,0 +1,27 @@ +import sequtils + +proc primeFactors(num:int):seq[int] = + var + c:int = 2 + n:int = num + while true: + if(n mod c) == 0: + result.add(c) + n = n div c + else: c += 1 + if n <= 1: break + +proc squareFreeIntegers() = + var + factors:seq[int] + i:int = 1 + while true: + factors = primeFactors(i) + if factors.len - deduplicate(factors).len == 0: + stdout.write(i, ' ') + i += 1 + if i >= 500: break + +when isMainModule: + squareFreeIntegers() + diff --git a/challenge-150/deadmarshal/pascal/ch1.pas b/challenge-150/deadmarshal/pascal/ch1.pas new file mode 100644 index 0000000000..2312b8eedc --- /dev/null +++ b/challenge-150/deadmarshal/pascal/ch1.pas @@ -0,0 +1,33 @@ +program Ch1; + +{$mode objfpc} +uses + SysUtils,GVector; + +type + TStringVec = specialize TVector<AnsiString>; + +var + Vec:TStringVec; + +function FibonacciWords(var Vec:TStringVec):Integer; +begin + repeat + Vec.PushBack(Vec[Vec.Size-2] + Vec[Vec.Size-1]); + until(Length(Vec[Vec.Size-1]) >= 51); + Result := StrToInt(Vec[Vec.Size-1][51]); +end; + +begin + if ParamCount < 2 then + begin + WriteLn(StdErr, 'Provide 2 string args!'); + Halt(1); + end; + Vec := TStringVec.Create; + Vec.PushBack(ParamStr(1)); + Vec.PushBack(ParamStr(2)); + WriteLn(FibonacciWords(Vec)); + FreeAndNil(Vec); +end. + diff --git a/challenge-150/deadmarshal/pascal/ch2.pas b/challenge-150/deadmarshal/pascal/ch2.pas new file mode 100644 index 0000000000..b3c2538a34 --- /dev/null +++ b/challenge-150/deadmarshal/pascal/ch2.pas @@ -0,0 +1,53 @@ +program Ch2; + +{$mode objfpc} +uses + SysUtils,GVector; + +type + TIntVec = specialize TVector<Integer>; + +function PrimeFactors(N:Integer):TIntVec; +var + C:Integer; +begin + Result := TIntVec.Create; + C := 2; + repeat + if N mod C = 0 then + begin + Result.PushBack(C); + N := N div C; + end + else Inc(C); + until(N <= 1); +end; + +function Uniq(constref Vec:TIntVec):Boolean; +var + I,J:Integer; +begin + Result := True; + for I := 0 to Pred(Vec.Size) do + for J := I+1 to Pred(Vec.Size) do + if Vec[I] = Vec[J] then Exit(False); +end; + +procedure SquareFreeIntegers; +var + Factors:TIntVec; + I:Integer; +begin + I := 1; + repeat + Factors := PrimeFactors(I); + if Uniq(Factors) then Write(I, ' '); + Inc(I); + FreeAndNil(Factors); + until(I >= 500); +end; + +begin + SquareFreeIntegers; +end. + diff --git a/challenge-150/deadmarshal/python/ch1.py b/challenge-150/deadmarshal/python/ch1.py new file mode 100644 index 0000000000..4a196ee1e0 --- /dev/null +++ b/challenge-150/deadmarshal/python/ch1.py @@ -0,0 +1,14 @@ +import sys + +if len(sys.argv) != 3: + sys.stderr.write("Provide 2 string args!") + exit(1) + +def fibonacci_words(input1,input2): + arr = [input1,input2] + while True: + arr.append(arr[-2] + arr[-1]) + if len(arr[-1]) >= 52: break + return arr[-1][50] + +print(fibonacci_words(sys.argv[1], sys.argv[2])) diff --git a/challenge-150/deadmarshal/python/ch2.py b/challenge-150/deadmarshal/python/ch2.py new file mode 100644 index 0000000000..bafa5b82d7 --- /dev/null +++ b/challenge-150/deadmarshal/python/ch2.py @@ -0,0 +1,16 @@ +# pip3 install primefac +from primefac import primefac + +def square_free_integers(): + arr = [] + i = 1 + while True: + primes = list(primefac(i)) + if(len(primes) - len(set(primes)) == 0): arr.append(i) + i += 1 + if i >= 500: break + return arr + +if __name__ == "__main__": + for elem in square_free_integers(): + print(elem, end=' ') diff --git a/challenge-150/deadmarshal/raku/ch-1.raku b/challenge-150/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..e85a262dce --- /dev/null +++ b/challenge-150/deadmarshal/raku/ch-1.raku @@ -0,0 +1,14 @@ +sub fibonacci_words($input1, $input2) +{ + my @arr = [$input1, $input2]; + repeat + { + @arr.push(@arr[*-2] ~ @arr[*-1]); + }until (@arr[*-1].chars >= 52); + return @arr.comb[50]; +} + +sub MAIN(Str $input1, Str $input2) +{ + say fibonacci_words($input1, $input2); +} diff --git a/challenge-150/deadmarshal/raku/ch-2.raku b/challenge-150/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..dc59233f16 --- /dev/null +++ b/challenge-150/deadmarshal/raku/ch-2.raku @@ -0,0 +1,17 @@ +use Prime::Factor; + +sub square-free-integers() +{ + my $i = 1; + while ($i <= 500) + { + my @pf = prime-factors($i); + print "$i " if @pf.elems - unique(@pf).elems == 0; + $i++; + } +} + +sub MAIN() +{ + square-free-integers; +} |
