summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <sancn@live.com>2017-05-23 17:02:13 +0200
committerSanto Cariotti <sancn@live.com>2017-05-23 17:02:13 +0200
commit8f203923ba6c0490fd5f2b974de6c9a2eeb07278 (patch)
treedc531fe32b7edac134f74800433120fe246db9d9
parentcdf76c80805cfc7c9dcf514ad4080d47b0fd16c0 (diff)
added first file in java
-rw-r--r--java/somme.java35
1 files changed, 35 insertions, 0 deletions
diff --git a/java/somme.java b/java/somme.java
new file mode 100644
index 0000000..c1da9d5
--- /dev/null
+++ b/java/somme.java
@@ -0,0 +1,35 @@
+import java.util.Scanner;
+
+public class HelloWorld {
+ private static boolean pari(int n) {
+ return (n % 2) == 0;
+ }
+
+ private static int sequenza(int n, boolean pollatz, int tCollatz) {
+ int m = ((pollatz) ? 5 : 3), tot = 1;
+
+ while(n != 1) {
+ n = (pari(n)) ? n/2 : n*m+1;
+
+ tot++;
+
+ if(tCollatz != -1 && tot > tCollatz)
+ break;
+ }
+
+ return tot;
+ }
+
+ public static void main(String[] args) {
+ Scanner in = new Scanner(System.in);
+ int N1 = in.nextInt(), N2 = in.nextInt(), tot = 0, collatz;
+
+ for(int i = N1; i <= N2; i++) {
+ collatz = sequenza(i, false, -1);
+ if(sequenza(i, true, collatz) < collatz )
+ tot++;
+ }
+
+ System.out.println(tot);
+ }
+}