# Spotify Puzzle has me... puzzled

Page 1 of 1

## 2 Replies - 379 Views - Last Post: 27 December 2012 - 12:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=304975&amp;s=f799ee88c5af3d6accf6e6d51fe2c2ff&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 recheej

Reputation: 0
• Posts: 22
• Joined: 26-December 12

# Spotify Puzzle has me... puzzled

Posted 27 December 2012 - 10:03 AM

Hello peeps again. I feel like I've been asking a lot of questions. I'm new to this forum so if there's any thing I'm doing wrong please let me know. But here's my dilemma. I'm trying to solve this problem located on the spotify website:

http://www.spotify.c...ticket-lottery/

It's very interesting. I finally got all the code up for it, and it seems like I am getting the right answer. But when I submit the code, they tell me it's wrong. Here's my code:

```import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;

/**
*
* @author Rechee Jozil
* Written 12/26/2012
*/

public class Internship {

public static void main(String[] args) throws IOException{

double denomenator = 0.0,  numerator = 0.0, probability = 0.0, pEntered = 0.0, winners = 0.0, tickets = 0.0, pGroup = 0.0;

//Array list to hold values.
ArrayList<Double> lotteryInput = new ArrayList<Double>();

//Add values entered from input stream to arraylist
System.out.println("Enter values");

//Split string up

//Parse each element in st into double
for(int i = 0; i < 4 ; i++){
}
r.close();

//Value checking
while(lotteryInput.get(0) < 1 || lotteryInput.get(0) > 1000){
System.out.println("Number of people in lottery cannot be less than 1 or more than 100. Enter number of people in lottery again.");
lotteryInput.set(0, pEntered);

}
while(lotteryInput.get(1) < 1 || lotteryInput.get(1) > lotteryInput.get(0)){
System.out.println("Number of winners cannot be less than 1 or greater than number of people in lottery. Enter number of winners again.");
lotteryInput.set(1, winners);
}
while(lotteryInput.get(2) < 1 || lotteryInput.get(2) > 100){
System.out.println("Number of tickets cannot be less than 1 or greater than 100. Enter numbef of tickets again. ");
lotteryInput.set(2, tickets);
}
while(lotteryInput.get(3) < 1 || lotteryInput.get(3) > lotteryInput.get(0)){
System.out.println("Number of people in your group cannot be less than 1 or greater than number of people that entered. Enter number of people in group again. ");
lotteryInput.set(3, pGroup);
}

//Close buffer
r.close();

pEntered = lotteryInput.get(0);
winners = lotteryInput.get(1);
tickets = lotteryInput.get(2);
pGroup = lotteryInput.get(3);

//Main for loop for calculation of probablility
for(int k = 1; k <= pGroup; k++){
denomenator = (double) (factorial(pEntered) / (double) (factorial(winners) * factorial(pEntered - winners)));
numerator = (double) (factorial(pGroup)/ (double) ((factorial(k) * factorial(pGroup - k))) * (double) ((factorial(pEntered - pGroup)) / (double) (factorial(winners - k) * factorial((pEntered - pGroup)- (winners - k)))));
probability =probability + ((numerator/ denomenator) * TicketProb(pGroup, k, tickets));
}

System.out.println(probability);

}

// static recursive factorial method
static double factorial(double n){
if (n == 0){
return 1;
}else{
return n * factorial(n-1);
}

}

static double TicketProb(double x, double y, double z){
if(x <= (y * z)){
return 1.0;
}
else{
return 0.0;
}
}

}

```

If you plug in the sample input given by spotify, my answers are the same. Except with the third sample input where mine is 1.0 and there's is 1.0 with 0 repeating.

I think I've covered all angles. I've edited the code several times with no avail. Any body have any insight on how I might solve this problem?

Is This A Good Question/Topic? 0

## Replies To: Spotify Puzzle has me... puzzled

### #2 SwiftStriker00

• No idea why my code works

Reputation: 435
• Posts: 1,602
• Joined: 25-December 08

## Re: Spotify Puzzle has me... puzzled

Posted 27 December 2012 - 11:52 AM

Now I would normally say you are right, and spotify's output is wrong, there is no need to repeat trailing zeros. But if you want to make them happy and give your answers to the power of 10^9. You can use decimal format:
```DecimalFormat df = new DecimalFormat("#.#########");
System.out.println( df.format( yourResult )) ;

```

### #3 recheej

Reputation: 0
• Posts: 22
• Joined: 26-December 12

## Re: Spotify Puzzle has me... puzzled

Posted 27 December 2012 - 12:11 PM

SwiftStriker00, on 27 December 2012 - 11:52 AM, said:

```DecimalFormat df = new DecimalFormat("#.#########");
System.out.println( df.format( yourResult )) ;

```

Hmm. It seems like the format object doesn't print out the last sample input right. I appreciate the help though. Seems like no one else has a clue so I'll just keep working on it. Might be something I missed.