Skip to main content
edited body
Source Link
peech
  • 319
  • 1
  • 4

Java, 67, 63 bytes

Golfed:

int x(int n,int p){return-((p%2<1)?p*p/2+p:p/2*(p+2)+1)+n-p*n;} 

Ungolfed:

int x(int n, int p) { return -((p%2<1) ? p*p/2+p : p/2 * (p+2) + 1) + n - p*n; } 

Basically I did some math on the formula. The n - p*n part takes care of the all n's in the formula. Then I used a super fun property of summing together linearly increasing set of integers (arithmetic series): I used the sum of first and last integer and then multiply it by set.length / 2 (I also check for the parity and handle it appropriately).

Try it: https://ideone.com/AnL03qhttps://ideone.com/DEd85A

Java, 67, 63 bytes

Golfed:

int x(int n,int p){return-((p%2<1)?p*p/2+p:p/2*(p+2)+1)+n-p*n;} 

Ungolfed:

int x(int n, int p) { return -((p%2<1) ? p*p/2+p : p/2 * (p+2) + 1) + n - p*n; } 

Basically I did some math on the formula. The n - p*n part takes care of the all n's in the formula. Then I used a super fun property of summing together linearly increasing set of integers (arithmetic series): I used the sum of first and last integer and then multiply it by set.length / 2 (I also check for the parity and handle it appropriately).

Try it: https://ideone.com/AnL03q

Java, 67, 63 bytes

Golfed:

int x(int n,int p){return-((p%2<1)?p*p/2+p:p/2*(p+2)+1)+n-p*n;} 

Ungolfed:

int x(int n, int p) { return -((p%2<1) ? p*p/2+p : p/2 * (p+2) + 1) + n - p*n; } 

Basically I did some math on the formula. The n - p*n part takes care of the all n's in the formula. Then I used a super fun property of summing together linearly increasing set of integers (arithmetic series): I used the sum of first and last integer and then multiply it by set.length / 2 (I also check for the parity and handle it appropriately).

Try it: https://ideone.com/DEd85A

added 31 characters in body
Source Link
peech
  • 319
  • 1
  • 4

Java, 6767, 63 bytes

Golfed:

int yx(int n, int p){return-((p%2==0p%2<1)?pp*p/2*(p+1)2+p:p/2*(p+2)+1)+n-p*n;} 

Ungolfed:

public static int x(int n, int p) { return -((p%2==0p%2<1) ? pp*p/2 * (p+1)2+p : p/2 * (p+2) + 1) + n - p*n; } 

Basically I did some math on the formula. The n - p*n part takes care of the all n's in the formula. Then I used a super fun property of summing together linearly increasing set of integers (arithmetic series): I used the sum of first and last integer and then multiply it by set.length / 2 (I also check for the parity and handle it appropriately).

Try it: https://ideone.com/AnL03q

Java, 67 bytes

Golfed:

int y(int n, int p){return-((p%2==0)?p/2*(p+1):p/2*(p+2)+1)+n-p*n;} 

Ungolfed:

public static int x(int n, int p) { return -((p%2==0) ? p/2 * (p+1) : p/2 * (p+2) + 1) + n - p*n; } 

Basically I did some math on the formula. The n - p*n part takes care of the all n's in the formula. Then I used a super fun property of summing together linearly increasing set of integers: I used the sum of first and last integer and then multiply it by set.length / 2 (I also check for the parity and handle it appropriately).

Try it: https://ideone.com/AnL03q

Java, 67, 63 bytes

Golfed:

int x(int n,int p){return-((p%2<1)?p*p/2+p:p/2*(p+2)+1)+n-p*n;} 

Ungolfed:

int x(int n, int p) { return -((p%2<1) ? p*p/2+p : p/2 * (p+2) + 1) + n - p*n; } 

Basically I did some math on the formula. The n - p*n part takes care of the all n's in the formula. Then I used a super fun property of summing together linearly increasing set of integers (arithmetic series): I used the sum of first and last integer and then multiply it by set.length / 2 (I also check for the parity and handle it appropriately).

Try it: https://ideone.com/AnL03q

added 1 character in body
Source Link
peech
  • 319
  • 1
  • 4

Java, 67 bytes

Golfed:

int y(int n, int p){return-((p%2==0)?p/2*(p+1):p/2*(p+2)+1)+n-p*n;} 

Ungolfed:

public static int x(int n, int p) { return -((p%2==0) ? p/2 * (p+1) : p/2 * (p+2) + 1) + n - p*n; } 

Basically I did some math on the formula. The n - p*n part takes care of the all n's in the formula. Then I used a super fun property of summing together linearly increasing set of integers: I used the sum of first and last integer and then multiply it by set.length / 2 (I also check for the parity and handle it appropriately).

Try it: https://ideone.com/AnL03q

Java, 67 bytes

Golfed:

int y(int n, int p){return-((p%2==0)?p/2*(p+1):p/2*(p+2)+1)+n-p*n;} 

Ungolfed:

public static int x(int n, int p) { return -((p%2==0) ? p/2 * (p+1) : p/2 * (p+2) + 1) + n - p*n; } 

Basically I did some math on the formula. The n - p*n part takes care of the all n's in the formula. Then I used a super fun property of summing together linearly increasing set of integers: I used the sum of first and last integer and then multiply it by set.length / 2 (I also check for the parity and handle it appropriately.

Try it: https://ideone.com/AnL03q

Java, 67 bytes

Golfed:

int y(int n, int p){return-((p%2==0)?p/2*(p+1):p/2*(p+2)+1)+n-p*n;} 

Ungolfed:

public static int x(int n, int p) { return -((p%2==0) ? p/2 * (p+1) : p/2 * (p+2) + 1) + n - p*n; } 

Basically I did some math on the formula. The n - p*n part takes care of the all n's in the formula. Then I used a super fun property of summing together linearly increasing set of integers: I used the sum of first and last integer and then multiply it by set.length / 2 (I also check for the parity and handle it appropriately).

Try it: https://ideone.com/AnL03q

Source Link
peech
  • 319
  • 1
  • 4
Loading