Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 37194

Java (not to be confused with JavaScript) is a class-based, object-oriented, strongly typed, reflective language and run-time environment (JRE). Java programs are compiled to bytecode and run in a virtual machine (JVM) enabling a "write once, run anywhere" (WORA) methodology.

1 vote

Determining if three numbers are consecutive

Observing that a and b must be close together, this code is not too bad: public static boolean consecutive(int a, int b, int c) { switch (b - a) { case +1: return c == b + 1 || c == a - 1 …
gnasher729's user avatar
  • 2,970
0 votes

Determining the difference between odd and even numbers

You can count just the even digits, or just the odd digits. If you count the odd ones, then clearly e = N.length - o, and instead of returning o - e you return 2 * o - N.length. And instead of an if …
gnasher729's user avatar
  • 2,970
26 votes
Accepted

Finding the number of integers that are a square and a cube at the same time

Let's say a = 1, b = 1,000,000. Your code is a nested loop, each loop iterating a million times, for a total of one trillion tests. And I tell, out of my head, that the result is 10 and the numbers ar …
gnasher729's user avatar
  • 2,970
8 votes

Collatz Sequence

Now since the whole point of this code is to check the Collatz conjecture (which says that whatever integer you start with, you'll eventually end up at 1, and you won't get a repeating cycle or number …
gnasher729's user avatar
  • 2,970
2 votes

Counting number of eights

You can quit the recursion when n < 8, which makes it a bit faster. The check for two consecutive eights is easily done by checking whether n % 100 = 88. public int count8(int n) { if (n < 8) retu …
gnasher729's user avatar
  • 2,970
1 vote

Checking whether a string is a valid number

Saying "is this string a valid number" is quite pointless when you don't have a spec what a valid number is. For example, a valid JSON number can have a leading minus but not a leading plus, it can't …
gnasher729's user avatar
  • 2,970
2 votes

Miller-Rabin Prime test (Speed is the main goal)

Several improvements: Consider that the test "if (p % 3 == 0)..." will identify 33.3% of all numbers as composite. If Miller-Rabin takes longer than three times to test whether p % 3 == 0, adding that …
gnasher729's user avatar
  • 2,970
1 vote

Sum of the digits is prime or not

Instead of checking numbers, check the sets of digits in sorted order. Let's say you are looking for six digit solutions. Sum of squares of digits is at most 6 x 81, so you first create an array bool …
gnasher729's user avatar
  • 2,970
4 votes

Using if else statements correctly

Names of variables should be chosen well. If you name a variable currentPage, I would assume that it contains a page number (1, 2, 3, 4, etc). If you want a boolean variable that is true if we are on …
gnasher729's user avatar
  • 2,970
27 votes

Yet another boring FizzBuzz

Congratulations. First, because you made it quite easily into the next round of your job interview. That's better than 80% to 90% of all candidates applying for the average software development job …
gnasher729's user avatar
  • 2,970
2 votes

Project Euler #4 - Largest Palindrome Product in Java

You don't want to find all pairs with a palindromic product, only the largest one. So you don't need to check pairs (i, j) where i*j is less than the largest palindrome that you found so far. But you …
gnasher729's user avatar
  • 2,970
0 votes

Ten thousand and first prime checker takes a very long time

Unless your compiler is quite clever, it will calculate Math.floor (Math.sqrt (num)) on every single iteration of the loop, which will probably take three times longer than the rest of the operations. …
gnasher729's user avatar
  • 2,970
1 vote

Longest palindrome in a string

What's the longest palindrome in "War and Peace"? I bet it's not very long. But if you look for the longest palindrome in a book of just a million characters, you are checking about 500 billion substr …
gnasher729's user avatar
  • 2,970
2 votes

Optimizing "Herd Sums" problem using dynamic programming

A group of consecutive integers adds up to N. The number of integers in that group is odd or even. You may have n integers, n odd. If the middle one is k, then the sum is n*k. You may have 2n inte …
gnasher729's user avatar
  • 2,970
0 votes

Comparing pivot choosing methods in quicksort

Random arrays are actually not that common. Lots of times you will be sorting an array that is already sorted (because someone was careless), or one that is almost sorted, or sorted in reverse order, …
gnasher729's user avatar
  • 2,970

15 30 50 per page