Skip to main content
Commonmark migration
Source Link

#Java

Java

void fizzy(int limit){
    String[] output = new String[limit];
    Arrays.fill(output,"");
    
    List<SimpleEntry<Integer,String>> tests = new ArrayList<SimpleEntry<Integer,String>>();
    tests.add(newEntry(3,"Fizz"));      
    tests.add(newEntry(4,"Jazz"));      
    tests.add(newEntry(5,"Buzz"));      

    for(SimpleEntry<Integer,String> test : tests)
        for(int i=test.getKey();i<limit;i+=test.getKey())
            output[i] += test.getValue();           
    
    for(int i=1;i<limit;i++)
        System.out.println(output[i].length()<1 ? i : output[i]);
}   

SimpleEntry<Integer,String> newEntry(int key, String value){
    return new SimpleEntry<Integer,String>(key,value);
}

So Java isn't really considered "beautiful" by most, but that's crazy subjective so I went by the guidelines in the question:

  • Don't Repeat Yourself: No problem. You only need to add one line for each number. I even made a helper function so you don't have to type as much when you do (Java can be a bit verbose sometimes, if you didn't know).
  • Efficiency of division/modulus operations: Perfect efficiency, since there is no modulus or division at all.

That's not to say that the algorithm as a whole is the most efficient (it's not), but I think it hits the bulleted points well.

#Java

void fizzy(int limit){
    String[] output = new String[limit];
    Arrays.fill(output,"");
    
    List<SimpleEntry<Integer,String>> tests = new ArrayList<SimpleEntry<Integer,String>>();
    tests.add(newEntry(3,"Fizz"));      
    tests.add(newEntry(4,"Jazz"));      
    tests.add(newEntry(5,"Buzz"));      

    for(SimpleEntry<Integer,String> test : tests)
        for(int i=test.getKey();i<limit;i+=test.getKey())
            output[i] += test.getValue();           
    
    for(int i=1;i<limit;i++)
        System.out.println(output[i].length()<1 ? i : output[i]);
}   

SimpleEntry<Integer,String> newEntry(int key, String value){
    return new SimpleEntry<Integer,String>(key,value);
}

So Java isn't really considered "beautiful" by most, but that's crazy subjective so I went by the guidelines in the question:

  • Don't Repeat Yourself: No problem. You only need to add one line for each number. I even made a helper function so you don't have to type as much when you do (Java can be a bit verbose sometimes, if you didn't know).
  • Efficiency of division/modulus operations: Perfect efficiency, since there is no modulus or division at all.

That's not to say that the algorithm as a whole is the most efficient (it's not), but I think it hits the bulleted points well.

Java

void fizzy(int limit){
    String[] output = new String[limit];
    Arrays.fill(output,"");
    
    List<SimpleEntry<Integer,String>> tests = new ArrayList<SimpleEntry<Integer,String>>();
    tests.add(newEntry(3,"Fizz"));      
    tests.add(newEntry(4,"Jazz"));      
    tests.add(newEntry(5,"Buzz"));      

    for(SimpleEntry<Integer,String> test : tests)
        for(int i=test.getKey();i<limit;i+=test.getKey())
            output[i] += test.getValue();           
    
    for(int i=1;i<limit;i++)
        System.out.println(output[i].length()<1 ? i : output[i]);
}   

SimpleEntry<Integer,String> newEntry(int key, String value){
    return new SimpleEntry<Integer,String>(key,value);
}

So Java isn't really considered "beautiful" by most, but that's crazy subjective so I went by the guidelines in the question:

  • Don't Repeat Yourself: No problem. You only need to add one line for each number. I even made a helper function so you don't have to type as much when you do (Java can be a bit verbose sometimes, if you didn't know).
  • Efficiency of division/modulus operations: Perfect efficiency, since there is no modulus or division at all.

That's not to say that the algorithm as a whole is the most efficient (it's not), but I think it hits the bulleted points well.

Ungolfed loops to appease the commentor :P
Source Link
Geobits
  • 19.7k
  • 4
  • 55
  • 125

#Java

void fizzy(int limit){
    String[] output = new String[limit];
    Arrays.fill(output,"");
    
    List<SimpleEntry<Integer,String>> tests = new ArrayList<SimpleEntry<Integer,String>>();
    tests.add(newEntry(3,"Fizz"));      
    tests.add(newEntry(4,"Jazz"));      
    tests.add(newEntry(5,"Buzz"));      

    for(SimpleEntry<Integer,String> test : tests)
        for(int i=0;i=test.getKey(i+=test);i<limit;i+=test.getKey())<limit;output[i]+=test
            output[i] += test.getValue());           
    
    for(int i=0;++i<limit;Systemi=1;i<limit;i++)
        System.out.println(output[i].length()<1 ? i : output[i]));
}   

SimpleEntry<Integer,String> newEntry(int key, String value){
    return new SimpleEntry<Integer,String>(key,value);
}

So Java isn't really considered "beautiful" by most, but that's crazy subjective so I went by the guidelines in the question:

  • Don't Repeat Yourself: No problem. You only need to add one line for each number. I even made a helper function so you don't have to type as much when you do (Java can be a bit verbose sometimes, if you didn't know).
  • Efficiency of division/modulus operations: Perfect efficiency, since there is no modulus or division at all.

That's not to say that the algorithm as a whole is the most efficient (it's not), but I think it hits the bulleted points well.

#Java

void fizzy(int limit){
    String[] output = new String[limit];
    Arrays.fill(output,"");
    
    List<SimpleEntry<Integer,String>> tests = new ArrayList<SimpleEntry<Integer,String>>();
    tests.add(newEntry(3,"Fizz"));      
    tests.add(newEntry(4,"Jazz"));      
    tests.add(newEntry(5,"Buzz"));      

    for(SimpleEntry<Integer,String> test : tests)
        for(int i=0;(i+=test.getKey())<limit;output[i]+=test.getValue());           
    
    for(int i=0;++i<limit;System.out.println(output[i].length()<1?i:output[i]));
}   

SimpleEntry<Integer,String> newEntry(int key, String value){
    return new SimpleEntry<Integer,String>(key,value);
}

So Java isn't really considered "beautiful" by most, but that's crazy subjective so I went by the guidelines in the question:

  • Don't Repeat Yourself: No problem. You only need to add one line for each number. I even made a helper function so you don't have to type as much when you do (Java can be a bit verbose sometimes, if you didn't know).
  • Efficiency of division/modulus operations: Perfect efficiency, since there is no modulus or division at all.

That's not to say that the algorithm as a whole is the most efficient (it's not), but I think it hits the bulleted points well.

#Java

void fizzy(int limit){
    String[] output = new String[limit];
    Arrays.fill(output,"");
    
    List<SimpleEntry<Integer,String>> tests = new ArrayList<SimpleEntry<Integer,String>>();
    tests.add(newEntry(3,"Fizz"));      
    tests.add(newEntry(4,"Jazz"));      
    tests.add(newEntry(5,"Buzz"));      

    for(SimpleEntry<Integer,String> test : tests)
        for(int i=test.getKey();i<limit;i+=test.getKey())
            output[i] += test.getValue();           
    
    for(int i=1;i<limit;i++)
        System.out.println(output[i].length()<1 ? i : output[i]);
}   

SimpleEntry<Integer,String> newEntry(int key, String value){
    return new SimpleEntry<Integer,String>(key,value);
}

So Java isn't really considered "beautiful" by most, but that's crazy subjective so I went by the guidelines in the question:

  • Don't Repeat Yourself: No problem. You only need to add one line for each number. I even made a helper function so you don't have to type as much when you do (Java can be a bit verbose sometimes, if you didn't know).
  • Efficiency of division/modulus operations: Perfect efficiency, since there is no modulus or division at all.

That's not to say that the algorithm as a whole is the most efficient (it's not), but I think it hits the bulleted points well.

Source Link
Geobits
  • 19.7k
  • 4
  • 55
  • 125

#Java

void fizzy(int limit){
    String[] output = new String[limit];
    Arrays.fill(output,"");
    
    List<SimpleEntry<Integer,String>> tests = new ArrayList<SimpleEntry<Integer,String>>();
    tests.add(newEntry(3,"Fizz"));      
    tests.add(newEntry(4,"Jazz"));      
    tests.add(newEntry(5,"Buzz"));      

    for(SimpleEntry<Integer,String> test : tests)
        for(int i=0;(i+=test.getKey())<limit;output[i]+=test.getValue());           
    
    for(int i=0;++i<limit;System.out.println(output[i].length()<1?i:output[i]));
}   

SimpleEntry<Integer,String> newEntry(int key, String value){
    return new SimpleEntry<Integer,String>(key,value);
}

So Java isn't really considered "beautiful" by most, but that's crazy subjective so I went by the guidelines in the question:

  • Don't Repeat Yourself: No problem. You only need to add one line for each number. I even made a helper function so you don't have to type as much when you do (Java can be a bit verbose sometimes, if you didn't know).
  • Efficiency of division/modulus operations: Perfect efficiency, since there is no modulus or division at all.

That's not to say that the algorithm as a whole is the most efficient (it's not), but I think it hits the bulleted points well.