# Factorials of ANY number, not just positive integers

Page 1 of 1

## 9 Replies - 1610 Views - Last Post: 03 May 2008 - 11:46 AMRate 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=50768&amp;s=b6079019da4282985a9817bc32b2ea56&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 herefishyfishy

Reputation: 7
• Posts: 60
• Joined: 01-May 08

# Factorials of ANY number, not just positive integers

Posted 01 May 2008 - 04:56 PM

I have been working on a Java calculator for quite a bit now, and I would like to be able to find the factorial of a number using only the Java API. However, I want the program to be able to calculate the factorial of any real number, not just natural numbers. (The definition of a factorial of a non-natural number can be found Here) This is my calculator so far. What code should I use for the FactorialListener class?

```import java.lang.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

class CalcConstruct {
public static void main(String[] args){
FrameConstruct y = new FrameConstruct("Calculator 3.0");
}
}
enum Operator {
}
class FrameConstruct extends JFrame {
static String left = "";
static String right = "";
static Operator oper = null;
static boolean isLeft = true;
static JTextField textfield = new JTextField();
static JButton a = new JButton("7");
JButton b = new JButton("8");
JButton c = new JButton("9");
JButton d = new JButton("/");
JButton e = new JButton("4");
JButton f = new JButton("5");
JButton g = new JButton("6");
JButton h = new JButton("*");
JButton i = new JButton("1");
JButton j = new JButton("2");
JButton k = new JButton("3");
JButton l = new JButton("+");
JButton m = new JButton("0");
JButton n = new JButton("=");
JButton o = new JButton("-");
JButton p = new JButton(".");//BUTTON INITILIZATION
JButton q = new JButton("+/-");
JButton pi= new JButton("\u03C0");
JButton to= new JButton("^");
JButton fa= new JButton("!")
FrameConstruct(String title) {
super(title);
setSize(300,300);
setResizable(false);
a.setFocusable(true);
a.requestFocusInWindow();
textfield.setEditable(false);
public void windowClosing(WindowEvent event){
System.exit(0);
}
});

GridBagLayout gbl = new GridBagLayout();

GridBagConstraints gbcbutt1= new GridBagConstraints();
GridBagConstraints gbcbutt2= new GridBagConstraints();
GridBagConstraints gbctext= new GridBagConstraints();
gbctext.gridwidth = GridBagConstraints.REMAINDER;
gbctext.fill = GridBagConstraints.BOTH;
gbctext.weighty = 1.0;
gbcbutt1.weightx = 1.0;
gbcbutt1.fill = GridBagConstraints.BOTH;
gbcbutt2.weightx = 1.0;
gbcbutt1.weighty = 1.0;
gbcbutt2.fill = GridBagConstraints.BOTH;
gbcbutt2.gridwidth = GridBagConstraints.REMAINDER;
gbcbutt2.weighty = 1.0;
gbl.setConstraints(a, gbcbutt1);
gbl.setConstraints(b, gbcbutt1);
gbl.setConstraints(c, gbcbutt1);
gbl.setConstraints(e, gbcbutt1);
gbl.setConstraints(f, gbcbutt1);
gbl.setConstraints(g, gbcbutt1);
gbl.setConstraints(i, gbcbutt1);
gbl.setConstraints(j, gbcbutt1);
gbl.setConstraints(k, gbcbutt1);
gbl.setConstraints(m, gbcbutt1);
gbl.setConstraints(q, gbcbutt1);
gbl.setConstraints(p, gbcbutt1);
gbl.setConstraints(pi,gbcbutt1);
gbl.setConstraints(to,gbcbutt1);
gbl.setConstrainte(fa,gbcbutt1);
gbl.setConstraints(d, gbcbutt2);
gbl.setConstraints(h, gbcbutt2);
gbl.setConstraints(l, gbcbutt2);
gbl.setConstraints(o, gbcbutt2);
gbl.setConstraints(textfield, gbctext);
gbl.setConstraints(n, gbcbutt2);

setLayout(gbl); //FRAME SETUP

public void keyReleased(KeyEvent event){
}
public void keyPressed(KeyEvent event){
}
public void keyTyped(KeyEvent ev){
char key1 = ev.getKeyChar();
switch (key1) {
case '0':m.doClick(); break;
case '1':i.doClick(); break;
case '2':j.doClick(); break;
case '3':k.doClick(); break;
case '4':e.doClick(); break;
case '5':f.doClick(); break;
case '6':g.doClick(); break;
case '7':a.doClick(); break;
case '8':b.doClick(); break;
case '9':c.doClick(); break;
case '+':l.doClick(); break;
case '-':o.doClick(); break;
case '*':h.doClick(); break;
case '/':d.doClick(); break;
case '.':p.doClick(); break;
case '=':
case '\n':n.doClick(); break;
case '!':fa.doClick(); break;
case '^':to.doClick(); break;
}
}
}
);

setVisible(true); //SHOW THE FRAME
FrameConstruct.a.requestFocusInWindow();
}
}
class FactorialListener implements ActionListener {
public void actionPerformed(ActionEvent event){
// WHAT SHOULD I PUT HERE?
}
}
class NumListener implements ActionListener { //STORE NUMBERS IN FrameConstruct.left AND FrameConstruct.right
public void actionPerformed(ActionEvent event){
String number = ((JButton)(event.getSource())).getText();
if(FrameConstruct.left==""){
FrameConstruct.textfield.setText("");
}
String initialtext = FrameConstruct.textfield.getText();
if(number=="+/-"){
number="-";
}
if(number=="\u03C0"){
number = "3.1415926535897932384626";
}

if(FrameConstruct.isLeft){
FrameConstruct.left += number;
}
else{
FrameConstruct.right += number;
}
FrameConstruct.textfield.setText(initialtext+number);
FrameConstruct.a.requestFocusInWindow();
}
}
class OperListener implements ActionListener { //STORE OPERATORS IN FrameConstruct.oper
public void actionPerformed(ActionEvent event){
String initialtext = FrameConstruct.textfield.getText();
String operator = ((JButton)(event.getSource())).getText();
FrameConstruct.isLeft = false;
if(operator.equals("/")){
FrameConstruct.oper = Operator.DIVIDE;
FrameConstruct.textfield.setText(initialtext+" / ");
} else if(operator.equals("*")){
FrameConstruct.oper = Operator.MULTIPLY;
FrameConstruct.textfield.setText(initialtext+" * ");
} else if(operator.equals("-")){
FrameConstruct.oper = Operator.SUBTRACT;
FrameConstruct.textfield.setText(initialtext+" - ");
} else if(operator.equals("+")){
FrameConstruct.textfield.setText(initialtext+" + ");
} else if(operator.equals("^")){
FrameConstruct.oper = Operator.EXPONENT;
FrameConstruct.textfield.setText(initialtext+" ^ ");
}
FrameConstruct.a.requestFocusInWindow();
}
}
strictfp class EqualListener implements ActionListener {//PERFORM OPERATIONS WHEN = IS PRESSED
public void actionPerformed(ActionEvent event){
try {
if(FrameConstruct.isLeft){
FrameConstruct.textfield.setText("No operator");
FrameConstruct.isLeft = true;
FrameConstruct.left="";
FrameConstruct.right="";
return;
}
java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
nf.setMaximumFractionDigits(9);
nf.setGroupingUsed(false);
double myLeft = (new Double(FrameConstruct.left)).doubleValue();
double myRight = (new Double(FrameConstruct.right)).doubleValue();
if(FrameConstruct.oper.equals(Operator.DIVIDE)){
FrameConstruct.textfield.setText(nf.format(myLeft)+" / "+nf.format(myRight)+" = "+nf.format(myLeft/myRight));
} else if(FrameConstruct.oper.equals(Operator.MULTIPLY)){
FrameConstruct.textfield.setText(nf.format(myLeft)+" * "+nf.format(myRight)+" = "+nf.format(myLeft*myRight));
} else if(FrameConstruct.oper.equals(Operator.SUBTRACT)){
FrameConstruct.textfield.setText(nf.format(myLeft)+" - "+nf.format(myRight)+" = "+nf.format(myLeft-myRight));
FrameConstruct.textfield.setText(nf.format(myLeft)+" + "+nf.format(myRight)+" = "+nf.format(myLeft+myRight));
} else if(FrameConstruct.oper.equals(Operator.EXPONENT)){
FrameConstruct.textfield.setText(nf.format(myLeft)+" ^ "+nf.format(myRight)+" = "+nf.format(Math.pow(myLeft,myRight)));
}
FrameConstruct.isLeft = true;
FrameConstruct.left="";
FrameConstruct.right="";
} catch (Exception e){
FrameConstruct.textfield.setText("Syntax error");
FrameConstruct.left="";
FrameConstruct.right="";
FrameConstruct.isLeft = true;
}
FrameConstruct.a.requestFocusInWindow();
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Factorials of ANY number, not just positive integers

### #2 spearfish

• Monkey in Training

Reputation: 10
• Posts: 746
• Joined: 10-March 08

## Re: Factorials of ANY number, not just positive integers

Posted 01 May 2008 - 05:02 PM

Quote

not just natural number

Are you saying that you have all positive integers worked out?

0! = 0

-1! = -(1!)
-2! = +(2!)
-3! = -(3!)
-4! = +(4!)

See where I'm going? Take the factorial regularly, and then if the number is negative and odd; inverse the answer.

### #3 herefishyfishy

Reputation: 7
• Posts: 60
• Joined: 01-May 08

## Re: Factorials of ANY number, not just positive integers

Posted 01 May 2008 - 05:19 PM

spearfish, on 1 May, 2008 - 05:02 PM, said:

Quote

not just natural number

Are you saying that you have all positive integers worked out?

0! = 0

-1! = -(1!)
-2! = +(2!)
-3! = -(3!)
-4! = +(4!)

See where I'm going? Take the factorial regularly, and then if the number is negative and odd; inverse the answer.

Actually, that is not how you take the factorial of a negative integer. The factorial of a negative integer is undefined. However, every other real number has a real factorial, including negative non-integral real numbers. I need a way to calculate the factorials of non-negative integers (easy), positive non-integral real numbers, and negative non-integral real numbers.

Come on, is anyone going to help me anytime soon?

P.S. 0! = 1.

This post has been edited by herefishyfishy: 01 May 2008 - 05:55 PM

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8032
• Posts: 31,202
• Joined: 06-March 08

## Re: Factorials of ANY number, not just positive integers

Posted 01 May 2008 - 08:47 PM

See PI a few places
but nothing really effective about calculating the factorial of a real number....

### #5 fsloke

• D.I.C Regular

Reputation: 25
• Posts: 410
• Joined: 19-December 07

## Re: Factorials of ANY number, not just positive integers

Posted 01 May 2008 - 10:57 PM

Are you kidding?

Can we factorial a negative number?

My calculator cannot work it....

### #6 herefishyfishy

Reputation: 7
• Posts: 60
• Joined: 01-May 08

## Re: Factorials of ANY number, not just positive integers

Posted 02 May 2008 - 03:32 PM

fsloke, on 1 May, 2008 - 10:57 PM, said:

Are you kidding?

Can we factorial a negative number?

My calculator cannot work it....

You cannot factorial a negative integer, but you can factorial non-integral negative reals. However, some calculators cannot do it...

### #7 skaoth

Reputation: 90
• Posts: 601
• Joined: 07-November 07

## Re: Factorials of ANY number, not just positive integers

Posted 02 May 2008 - 04:37 PM

I don't think there is an easy way to calculate these values. You are going to have to implement the
gamma function and perform the limit.

There is an approximation function that is provided on wikipedia for non-integer (positive value) factorials
http://en.wikipedia.org/wiki/Factorial

The runtime is exponential buts that's expected. The function in gamma1 should
be easy to implement in java. This is just an approximation mind you. I don't know if this approximation
works for negative values as I haven't tried it. It does seem to work for positive values though

### #8 herefishyfishy

Reputation: 7
• Posts: 60
• Joined: 01-May 08

## Re: Factorials of ANY number, not just positive integers

Posted 03 May 2008 - 05:18 AM

skaoth, on 2 May, 2008 - 04:37 PM, said:

I don't think there is an easy way to calculate these values. You are going to have to implement the
gamma function and perform the limit.

There is an approximation function that is provided on wikipedia for non-integer (positive value) factorials
http://en.wikipedia.org/wiki/Factorial

The runtime is exponential buts that's expected. The function in gamma1 should
be easy to implement in java. This is just an approximation mind you. I don't know if this approximation
works for negative values as I haven't tried it. It does seem to work for positive values though

I tried using the gamma function approximation, and my answers were VERY inaccurate. So then I tried using
```class FactorialListener implements ActionListener {
public void actionPerformed(ActionEvent event){
try{
double myLeft = (new Double(FrameConstruct.left)).doubleValue();
double result = 1;
int i;
result=Math.exp(myLeft*Math.log(myLeft)-myLeft+Math.log(myLeft*(1+4*myLeft*(1+2*myLeft)))/6+Math.log(Math.PI)/2);
java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
nf.setMaximumFractionDigits(9);
nf.setGroupingUsed(false);
FrameConstruct.textfield.setText(nf.format(myLeft)+"! ="+nf.format(result));
FrameConstruct.isLeft = true;
FrameConstruct.left="";
FrameConstruct.right="";
} catch (Exception e){
FrameConstruct.textfield.setText("Syntax error");
FrameConstruct.left="";
FrameConstruct.right="";
FrameConstruct.isLeft = true;
}
FrameConstruct.a.requestFocusInWindow();
}
}
```

and it works, but it calculates 10! as 3628797.914471705, while its real value is 3628800.

This post has been edited by herefishyfishy: 03 May 2008 - 05:19 AM

### #9 baavgai

• Dreaming Coder

Reputation: 4892
• Posts: 11,287
• Joined: 16-October 07

## Re: Factorials of ANY number, not just positive integers

Posted 03 May 2008 - 06:14 AM

herefishyfishy, on 3 May, 2008 - 08:18 AM, said:

and it works, but it calculates 10! as 3628797.914471705, while its real value is 3628800.

Floating point operations in computers are always a little quirky. This is know and there's not a whole lot you can do about it, except allow for it.

Implement a function that does a standard factorial calculation for positive integers. Only use the approximation formula on those that can not be solved otherwise.

### #10 herefishyfishy

Reputation: 7
• Posts: 60
• Joined: 01-May 08

## Re: Factorials of ANY number, not just positive integers

Posted 03 May 2008 - 11:46 AM

Thank you!

Page 1 of 1

 Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).