summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorSanto Cariotti <sancn@live.com>2017-05-15 18:02:39 +0200
committerSanto Cariotti <sancn@live.com>2017-05-15 18:02:39 +0200
commit46d6c42c81be3c2b78254e009945578e10fab611 (patch)
tree916299aa23e0e1e67b5108ac92fbda01c6c3edad /cpp
parentd94941de70fb1dd815247818ae32cb20022f6e0e (diff)
added somme from GATOR contest
Diffstat (limited to 'cpp')
-rw-r--r--cpp/somme.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/cpp/somme.cpp b/cpp/somme.cpp
new file mode 100644
index 0000000..e42ad64
--- /dev/null
+++ b/cpp/somme.cpp
@@ -0,0 +1,63 @@
+/* gator 2017
+input: 1 5
+output: 1
+*/
+
+#include <iostream>
+#include <fstream>
+
+bool pari(int n)
+{
+ return ((n % 2) == 0) ? true : false;
+}
+
+int sequenza(int n, bool pollatz, int tCollatz = -1)
+{
+ int m;
+
+ if(pollatz)
+ m = 5;
+ else
+ m = 3;
+
+ int tot = 1;
+
+
+ while(n != 1) {
+ if(pari(n))
+ n /= 2;
+ else
+ n = n*m+1;
+
+
+ tot++;
+
+ if(tCollatz != -1 && tot > tCollatz)
+ break;
+
+ }
+
+ return tot;
+}
+
+int main()
+{
+ std::ifstream in("input.txt");
+ std::ofstream out("output.txt");
+
+ int N1, N2, tot = 0, collatz;
+ in >> N1 >> N2;
+
+ for(int i = N1; i <= N2; i++) {
+ collatz = sequenza(i, false);
+ if(sequenza(i, true, collatz) < collatz)
+ tot++;
+ }
+
+ out << tot;
+
+ out.close();
+ in.close();
+
+ return 0;
+} \ No newline at end of file