Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

#Java 8, (削除) 123 (削除ここまで)(削除) 149 (削除ここまで)(削除) 113 (削除ここまで) 108 bytes

Java 8, (削除) 123 (削除ここまで)(削除) 149 (削除ここまで)(削除) 113 (削除ここまで) 108 bytes

#Java 8, (削除) 123 (削除ここまで)(削除) 149 (削除ここまで)(削除) 113 (削除ここまで) 108 bytes

Java 8, (削除) 123 (削除ここまで)(削除) 149 (削除ここまで)(削除) 113 (削除ここまで) 108 bytes

-5 bytes again thanks to OliverGrégoire
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394

#Java 8, (削除) 123 (削除ここまで) (削除) 149 (削除ここまで) 113(削除) 113 (削除ここまで) 108 bytes

()->{for(int n=2,s=0n=0,e=7s=1,i;s!=e;n+=2,e+=7=n*7;){for(i=n,s=n;ii=s=++n*2;i-->0;s+=Math.ran‌​domrandom()*6);if(s!=e=n*7)Syst‌​emSystem.out.println(nn*2);}}

Or 112107 bytes if we use an Object null as unused parameter instead.

+26 bytes for a bug-fix, correctly pointed out by @Jules in the comments.
-3641 bytes thanks to @OliverGrégoire's great thinking!

Try it here. Try it here.

()->{ // Method without parameter nor return-type
 for(int n=2n=0, // Amount of dice, starting at 2
 s=0s=1, // Sum
 e=7,  // Equals check integer, starting at 7 (<- 3.5*2)Sum
 i; // Index
 s!=e; =n*7;){ // Loop (1) as long as the sum isn'tdoesn't theequal same`n`*7,
 as the equal-int
 n+=2,e+=7){ // And add two dice after every iteration
// because we for(i=nroll the dice per two, and 3.5*2=7
  for(i=s=++n*2;  // Reset both the index and sum to `n``n`*2,
 s=n; // And sum as well,//  so we can use random 0-5, instead of 1-6
 // soand we won't have to use `+1` at `Math.random()*6+1`*6`
 i-->0; // Inner loop (2) over the amount of dice
 s+=Math.ran‌​domrandom()*6 // And rollincrease themthe (randomly)sum with their random results
 ); // End of inner loop (2)
 if(s!=e=n*7) // If the sum isn't the same as thedoesn't equal-int `n`*7
 Syst‌​emSystem.out.println(nn*2); // Print the amount of dice for this iteration 
 } // End of loop (1)
} // End of method

#Java 8, (削除) 123 (削除ここまで) (削除) 149 (削除ここまで) 113 bytes

()->{for(int n=2,s=0,e=7,i;s!=e;n+=2,e+=7){for(i=n,s=n;i-->0;s+=Math.ran‌​dom()*6);if(s!=e)Syst‌​em.out.println(n);}}

Or 112 bytes if we use an Object null as unused parameter instead.

+26 bytes for a bug-fix, correctly pointed out by @Jules in the comments.
-36 bytes thanks to @OliverGrégoire's great thinking!

Try it here.

()->{ // Method without parameter nor return-type
 for(int n=2, // Amount of dice, starting at 2
 s=0, // Sum
 e=7,  // Equals check integer, starting at 7 (<- 3.5*2)
 i; // Index
 s!=e;  // Loop (1) as long as sum isn't the same as the equal-int
 n+=2,e+=7){ // And add two dice after every iteration
 for(i=n, // Reset the index to `n`
 s=n; // And sum as well, so we can use random 0-5 instead of 1-6
 // so we won't have to use `Math.random()*6+1`
 i-->0; // Inner loop (2) the amount of dice
 s+=Math.ran‌​dom()*6 // And roll them (randomly)
 ); // End of inner loop (2)
 if(s!=e) // If the sum isn't the same as the equal-int
 Syst‌​em.out.println(n); // Print the amount of dice
 } // End of loop (1)
} // End of method

#Java 8, (削除) 123 (削除ここまで) (削除) 149 (削除ここまで) (削除) 113 (削除ここまで) 108 bytes

()->{for(int n=0,s=1,i;s!=n*7;){for(i=s=++n*2;i-->0;s+=Math.random()*6);if(s!=n*7)System.out.println(n*2);}}

Or 107 bytes if we use an Object null as unused parameter instead.

+26 bytes for a bug-fix, correctly pointed out by @Jules in the comments.
-41 bytes thanks to @OliverGrégoire's great thinking!

Try it here.

()->{ // Method without parameter nor return-type
 for(int n=0, // Amount of dice
 s=1, // Sum
 i; // Index
 s!=n*7;){ // Loop (1) as long as the sum doesn't equal `n`*7,
 // because we roll the dice per two, and 3.5*2=7
  for(i=s=++n*2;  // Reset both the index and sum to `n`*2,
 //  so we can use random 0-5, instead of 1-6
 // and we won't have to use `+1` at `Math.random()*6`
 i-->0; // Inner loop (2) over the amount of dice
 s+=Math.random()*6 // And increase the sum with their random results
 ); // End of inner loop (2)
 if(s!=n*7) // If the sum doesn't equal `n`*7
 System.out.println(n*2); // Print the amount of dice for this iteration 
 } // End of loop (1)
} // End of method
-36 bytes thanks to OliverGrégoire
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394

#Java 8, 123(削除) 123 (削除ここまで)(削除) 149 (削除ここまで) 113 bytes

()->{for(int n=2,r=12s=0,i;fore=7,i;s!=e;n+=2,e+=7){for(r*=Mathi=n,s=n;i-->0;s+=Math.randomran‌​dom()+2;r!=3.5*n;n+=2*6)for;if(Systems!=e)Syst‌​em.out.println(n),r=0,i=0;i++<n+2;r+=1+Math.random()*6);}}

Or 122112 bytes if we use an Object null as unused parameter instead.

+26 bytes for a bug-fix, correctly pointed out by @Jules in the comments.
-36 bytes thanks to @OliverGrégoire's great thinking!

Try it here. Try it here.

()->{ // Method without parameter nor return-type
 for(int n=2,r=12,i; // SomeAmount tempof integersdice, starting at 2
 for s=0, // Sum
 e=7, // Equals check integer, starting at 7 (r*=Math<- 3.random(5*2)+2; // Start byi; rolling two dice // Index
 rs!=3.5*n;=e;  // Loop (1) as long as sum isn't the sumsame `r`as doesn'tthe equal 3.5*`n`-int
 n+=2,e+=7){ // And increase `n`add bytwo 2dice after every iteration
 for(System.out.println(n)i=n, // PrintReset the index to `n`
 r=0s=n; // And sum as well,i=0; so we can use random 0-5 instead of 1-6
  // Reset `r`so andwe `i`won't have to 0use `Math.random()*6+1`
 i++<n+2;i-->0; // Inner loop (2) the amount of dice + 2
 r+=1+Maths+=Math.randomran‌​dom()*6 // Roll each, andAnd addroll them to the sum `r`(randomly)
 ); // End of inner loop (2)
 if(s!=e) // If the sum isn't the same as the equal-int
 // End of loop Syst‌​em.out.println(1n); (implicit // single-line body) Print the amount of dice
} //  End of loop (1)
}  // End of method

Example run explanation:

  1. Start n at 2 (int n=2)
  2. Roll two dice at once, by picking a random number from 2-12 (r=12 and r*=Math.random()+2)
  3. Check if this random number is equal to 3.5*n (r!=3.5*n)
    1. If it is 3.5*n, leave the loop and we're done.
    2. If it is not 3.5*n: continue with step 4 below
  4. Print n (System.out.println(n))
  5. Then roll n+2 dice again (by picking a random number from 1-6) and save them in r (for(...,r=0,i=0;i++<n+2;r+=1+Math.random()*6);)
  6. And raise n by 2 (n+=2)
  7. And then get back to step 3 again by checking if n (now 4) times 3.5 equals r

#Java 8, 123 bytes

()->{int n=2,r=12,i;for(r*=Math.random()+2;r!=3.5*n;n+=2)for(System.out.println(n),r=0,i=0;i++<n+2;r+=1+Math.random()*6);}

Or 122 bytes if we use an Object null as unused parameter instead.

Try it here.

()->{ // Method without parameter nor return-type
 int n=2,r=12,i; // Some temp integers
 for(r*=Math.random()+2; // Start by rolling two dice
 r!=3.5*n; // Loop (1) as long as the sum `r` doesn't equal 3.5*`n`
 n+=2) // And increase `n` by 2 after every iteration
 for(System.out.println(n), // Print `n`
 r=0,i=0; // Reset `r` and `i` to 0
 i++<n+2; // Inner loop (2) the amount of dice + 2
 r+=1+Math.random()*6 // Roll each, and add them to the sum `r`
 ); // End of inner loop (2)
 // End of loop (1) (implicit / single-line body)
} // End of method

Example run explanation:

  1. Start n at 2 (int n=2)
  2. Roll two dice at once, by picking a random number from 2-12 (r=12 and r*=Math.random()+2)
  3. Check if this random number is equal to 3.5*n (r!=3.5*n)
    1. If it is 3.5*n, leave the loop and we're done.
    2. If it is not 3.5*n: continue with step 4 below
  4. Print n (System.out.println(n))
  5. Then roll n+2 dice again (by picking a random number from 1-6) and save them in r (for(...,r=0,i=0;i++<n+2;r+=1+Math.random()*6);)
  6. And raise n by 2 (n+=2)
  7. And then get back to step 3 again by checking if n (now 4) times 3.5 equals r

#Java 8, (削除) 123 (削除ここまで)(削除) 149 (削除ここまで) 113 bytes

()->{for(int n=2,s=0,e=7,i;s!=e;n+=2,e+=7){for(i=n,s=n;i-->0;s+=Math.ran‌​dom()*6);if(s!=e)Syst‌​em.out.println(n);}}

Or 112 bytes if we use an Object null as unused parameter instead.

+26 bytes for a bug-fix, correctly pointed out by @Jules in the comments.
-36 bytes thanks to @OliverGrégoire's great thinking!

Try it here.

()->{ // Method without parameter nor return-type
 for(int n=2, // Amount of dice, starting at 2
  s=0, // Sum
 e=7, // Equals check integer, starting at 7 (<- 3.5*2) i;  // Index
 s!=e;  // Loop (1) as long as sum isn't the same as the equal-int
 n+=2,e+=7){ // And add two dice after every iteration
 for(i=n, // Reset the index to `n`
 s=n; // And sum as well, so we can use random 0-5 instead of 1-6
  // so we won't have to use `Math.random()*6+1`
 i-->0; // Inner loop (2) the amount of dice
 s+=Math.ran‌​dom()*6 // And roll them (randomly)
 ); // End of inner loop (2)
 if(s!=e) // If the sum isn't the same as the equal-int
 Syst‌​em.out.println(n); //  Print the amount of dice
} //  End of loop (1)
}  // End of method
formatting the example run explanation with markdown
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
Loading
+1 byte due to bugfix (and no further change for a second bugfix..)
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394
Loading
bugfix
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394
Loading
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394
Loading

AltStyle によって変換されたページ (->オリジナル) /