summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIru Cai <mytbk920423@gmail.com>2018-05-26 14:27:47 +0800
committerIru Cai <mytbk920423@gmail.com>2018-05-26 14:27:47 +0800
commitc98d66aca04d0d839833b6b81bf249822c4ca350 (patch)
treedafe2a5a36384ab7feab7187271b4be9bba4fdc3
parentd5c645bf83f144838e84b21e4941a0e42b0a9a61 (diff)
downloadproject_euler-c98d66aca04d0d839833b6b81bf249822c4ca350.tar.xz
53, 55, 56
-rw-r--r--euler53.c26
-rw-r--r--euler55.scala20
-rw-r--r--euler56.scala24
3 files changed, 70 insertions, 0 deletions
diff --git a/euler53.c b/euler53.c
new file mode 100644
index 0000000..3e71c87
--- /dev/null
+++ b/euler53.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+
+/* number of r s.t. C(n, r)>x */
+int nr_ncr_bigger(int n, int x)
+{
+ int prod = 1;
+ int r = 0;
+
+ while (prod <= x && r*2 < n) {
+ r++;
+ prod = prod * (n-r+1) / r;
+ }
+
+ if (prod <= x)
+ return 0;
+ else
+ return n - r*2 + 1;
+}
+
+int main()
+{
+ int cnt = 0;
+ for (int i = 1; i <= 100; i++)
+ cnt += nr_ncr_bigger(i, 1000000);
+ printf("%d\n", cnt);
+}
diff --git a/euler55.scala b/euler55.scala
new file mode 100644
index 0000000..a36b6cd
--- /dev/null
+++ b/euler55.scala
@@ -0,0 +1,20 @@
+object Lychrel
+{
+ def lychrel(a: Int): Boolean = {
+ var t: BigInt = a
+ for (i <- 1 until 50) {
+ t = t + BigInt(t.toString.reverse)
+ if (t.toString == t.toString.reverse)
+ return false
+ }
+ true
+ }
+
+ def main(args: Array[String]) = {
+ var cnt = 0
+ for (i <- 1 until 10000)
+ if (lychrel(i)) cnt = cnt + 1
+
+ println(cnt)
+ }
+}
diff --git a/euler56.scala b/euler56.scala
new file mode 100644
index 0000000..a302b25
--- /dev/null
+++ b/euler56.scala
@@ -0,0 +1,24 @@
+object Lychrel
+{
+ def digital_sum(a: BigInt): Int = {
+ var sum: Int = 0
+ var t = a
+ while (t > 0) {
+ sum = sum + (t % 10).toInt
+ t = t / 10
+ }
+ sum
+ }
+
+ def main(args: Array[String]) = {
+ var maxsum = 0
+ for (a <- 2 until 100) {
+ var t: BigInt = 1
+ for (b <- 1 until 100) {
+ t = t * a
+ maxsum = maxsum max digital_sum(t)
+ }
+ }
+ println(maxsum)
+ }
+}