aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-07-06 21:41:43 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-07-06 21:41:43 +0100
commit5aca4db735e55e7b8cfb535a0edc2b9874e368c0 (patch)
tree2974e38e6dd4516d0dd9a383d76be4c00166244b
parentbe7e82af51253a1a2bab904de281633915e7de46 (diff)
downloadperlweeklychallenge-club-5aca4db735e55e7b8cfb535a0edc2b9874e368c0.tar.gz
perlweeklychallenge-club-5aca4db735e55e7b8cfb535a0edc2b9874e368c0.tar.bz2
perlweeklychallenge-club-5aca4db735e55e7b8cfb535a0edc2b9874e368c0.zip
Add Fortran solution to challenge 119
-rw-r--r--challenge-119/paulo-custodio/fortran/ch-1.f9039
-rw-r--r--challenge-119/paulo-custodio/fortran/ch-2.f9083
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-119/paulo-custodio/fortran/ch-1.f90 b/challenge-119/paulo-custodio/fortran/ch-1.f90
new file mode 100644
index 0000000000..ac7f8fa924
--- /dev/null
+++ b/challenge-119/paulo-custodio/fortran/ch-1.f90
@@ -0,0 +1,39 @@
+! Challenge 119
+!
+! TASK #1 - Swap Nibbles
+! Submitted by: Mohammad S Anwar
+! You are given a positive integer $N.
+!
+! Write a script to swap the two nibbles of the binary representation of the
+! given number and print the decimal number of the new binary representation.
+!
+! A nibble is a four-bit aggregation, or half an octet.
+!
+! To keep the task simple, we only allow integer less than or equal to 255.
+!
+! Example
+! Input: $N = 101
+! Output: 86
+!
+! Binary representation of decimal 101 is 1100101 or as 2 nibbles (0110)(0101).
+! The swapped nibbles would be (0101)(0110) same as decimal 86.
+!
+! Input: $N = 18
+! Output: 33
+!
+! Binary representation of decimal 18 is 10010 or as 2 nibbles (0001)(0010).
+! The swapped nibbles would be (0010)(0001) same as decimal 33.
+
+program ch1
+ implicit none
+
+ integer :: n, stat
+ character(len=50) :: arg
+
+ call get_command_argument(1, arg)
+ if (len_trim(arg) /= 0) then
+ read(arg,*,iostat=stat) n
+ n = ((mod((n / 16), 16)) + (mod(n, 16) * 16))
+ print *, n
+ end if
+end program ch1
diff --git a/challenge-119/paulo-custodio/fortran/ch-2.f90 b/challenge-119/paulo-custodio/fortran/ch-2.f90
new file mode 100644
index 0000000000..ea5555091e
--- /dev/null
+++ b/challenge-119/paulo-custodio/fortran/ch-2.f90
@@ -0,0 +1,83 @@
+! Challenge 119
+!
+! TASK #2 - Sequence without 1-on-1
+! Submitted by: Cheok-Yin Fung
+! Write a script to generate sequence starting at 1. Consider the increasing
+! sequence of integers which contain only 1's, 2's and 3's, and do not have any
+! doublets of 1's like below. Please accept a positive integer $N and print the
+! $Nth term in the generated sequence.
+!
+! 1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131, ...
+!
+! Example
+! Input: $N = 5
+! Output: 13
+!
+! Input: $N = 10
+! Output: 32
+!
+! Input: $N = 60
+! Output: 2223
+
+program ch2
+ implicit none
+
+ integer :: i, num, n, stat
+ character(len=50) :: arg
+
+ call get_command_argument(1, arg)
+ if (len_trim(arg) /= 0) then
+ read(arg,*,iostat=stat) num
+
+ n = 0
+ do i = 1, num
+ n = next_seq(n)
+ end do
+
+ print *, n
+ end if
+
+contains
+ function num_ok(n1)
+ logical :: num_ok
+ integer, intent(in) :: n1
+ integer :: n, digit, last_digit
+ logical :: failed
+
+ n = n1
+ failed = .false.
+ if (n <= 0) then
+ failed = .true.
+ end if
+
+ digit = 0
+ do while (n > 0)
+ last_digit = digit
+ digit = mod(n, 10)
+ n = n / 10
+ if (digit < 1 .or. digit > 3 .or. (digit == 1 .and. last_digit == 1)) then
+ failed = .true.
+ end if
+ end do
+
+ num_ok = .not. failed
+ end function num_ok
+
+ function next_seq(n1)
+ integer :: next_seq
+ integer, intent(in) :: n1
+ integer :: n
+
+ n = n1
+ n = n+1
+ do while (.true.)
+ if (num_ok(n)) then
+ exit
+ else
+ n = n+1
+ end if
+ end do
+
+ next_seq = n
+ end function next_seq
+end program ch2