aboutsummaryrefslogtreecommitdiff
path: root/challenge-164
diff options
context:
space:
mode:
authordeadmarshal <adeadmarshal@gmail.com>2022-07-02 11:44:50 +0430
committerdeadmarshal <adeadmarshal@gmail.com>2022-07-02 11:44:50 +0430
commit474df6b71ae4694b97fc69d1a494d3da1086cd00 (patch)
tree180ee816e75e61a66e2233a4cc95ad4d7b84e67f /challenge-164
parent5f74fc73c0fa3ab212350ffa9241e224549b807a (diff)
downloadperlweeklychallenge-club-474df6b71ae4694b97fc69d1a494d3da1086cd00.tar.gz
perlweeklychallenge-club-474df6b71ae4694b97fc69d1a494d3da1086cd00.tar.bz2
perlweeklychallenge-club-474df6b71ae4694b97fc69d1a494d3da1086cd00.zip
Added dlang and made corrections to the ch-2.cpp.
Diffstat (limited to 'challenge-164')
-rw-r--r--challenge-164/deadmarshal/cpp/ch-2.cpp4
-rw-r--r--challenge-164/deadmarshal/d/ch1.d29
-rw-r--r--challenge-164/deadmarshal/d/ch2.d44
3 files changed, 75 insertions, 2 deletions
diff --git a/challenge-164/deadmarshal/cpp/ch-2.cpp b/challenge-164/deadmarshal/cpp/ch-2.cpp
index c2ecc53b88..cf0ef595f1 100644
--- a/challenge-164/deadmarshal/cpp/ch-2.cpp
+++ b/challenge-164/deadmarshal/cpp/ch-2.cpp
@@ -20,8 +20,8 @@ bool is_happy(int n)
{
map[n]++;
n = sum_squares(n);
- if(n == 1) return 1;
- if(map[n] != 0) return 0;
+ if(n == 1) return true;
+ if(map[n] != 0) return false;
}
}
diff --git a/challenge-164/deadmarshal/d/ch1.d b/challenge-164/deadmarshal/d/ch1.d
new file mode 100644
index 0000000000..329b97fd3c
--- /dev/null
+++ b/challenge-164/deadmarshal/d/ch1.d
@@ -0,0 +1,29 @@
+import std.stdio;
+import std.math:sqrt;
+
+bool is_prime(int n)
+{
+ if(n <= 1) return false;
+ for(int i = 2; i <= cast(int)sqrt(cast(float)n); ++i)
+ if(n % i == 0) return false;
+ return true;
+}
+
+int reverse_num(int n)
+{
+ int rev = 0, rem;
+ while(n)
+ {
+ rem = n % 10;
+ rev = rem + (rev * 10);
+ n /= 10;
+ }
+ return rev;
+}
+
+void main()
+{
+ foreach(int i; 1..1000)
+ if((i == reverse_num(i)) && (is_prime(i)))
+ write(i, ' ');
+}
diff --git a/challenge-164/deadmarshal/d/ch2.d b/challenge-164/deadmarshal/d/ch2.d
new file mode 100644
index 0000000000..6804a88c54
--- /dev/null
+++ b/challenge-164/deadmarshal/d/ch2.d
@@ -0,0 +1,44 @@
+import std.stdio;
+import std.math:pow;
+
+int sum_squares(int n)
+{
+ int sum = 0;
+ while(n)
+ {
+ sum += cast(int)pow(n % 10, 2);
+ n /= 10;
+ }
+ return sum;
+}
+
+bool is_happy(int n)
+{
+ int[int] map;
+ while(1)
+ {
+ map[n]++;
+ n = sum_squares(n);
+ if(n == 1) return true;
+ if(n in map) return false;
+ }
+}
+
+void happy_numbers()
+{
+ int i = 0, count = 0;
+ while(count < 8)
+ {
+ if(is_happy(i))
+ {
+ write(i, ' ');
+ count++;
+ }
+ i++;
+ }
+}
+
+void main()
+{
+ happy_numbers();
+}