# The strange syntax of \`write(\*,’(A,$)’)\` used in the calculation of the factorial

**URL:** <https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124>\
**Category:** Uncategorized\
**Created:** [April 4, 2022, 7:29am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124 "2022-04-04T07:29:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hongyi](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hongyi](https://fortran-lang.discourse.group/u/Hongyi)\
**Post date:** [April 4, 2022, 7:29am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/1 "2022-04-04T07:29:58Z")

</div>

I noticed the following code snippet [here](https://www.physicsforums.com/threads/factorial-fortran-90.816501/):

```auto
program torial
    implicit none
   
    integer, parameter :: IK = selected_int_kind(18)
    integer(IK) :: i
     
    write(*,'(A,$)') 'Number? : '
    read(*,*) i
    write(*,*) 'Factorial is ',factorial(i)

contains

    recursive function factorial( n ) result (f)
        integer(IK) :: f, n
       
        if( n == 1 ) then
          f = 1
        else
          f = n * factorial( n - 1_IK )
        end if
       
    end function factorial
   
end program torial

```

What’s the meaning of the `write(*,'(A,$)')` used above?

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [April 4, 2022, 7:35am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/2 "2022-04-04T07:35:35Z")

</div>

A common extension at one time that there is now a standard implementation for. Use

```fortran
write(*,'(a)',advance='no')'Number? : '

```

now. The “$” surpressed advancing to a new line after writing the output, the same as  
now provided by “advance=‘no’”.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [April 4, 2022, 7:46am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/3 "2022-04-04T07:46:01Z")

</div>

Note that this $ looks like the $ that marks the end of a line in a regular expression.

---

<div class="post-metadata">

**Author:** ![Hongyi](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hongyi](https://fortran-lang.discourse.group/u/Hongyi)\
**Post date:** [April 4, 2022, 8:14am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/4 "2022-04-04T08:14:35Z")

</div>

> [@urbanjost](#):
>
> `write(*,'(a)',advance='no')'Number? : '`

Thank you for your explanation. I find the following description in [Intel ® Fortran Compiler Classic and Intel ® Fortran Compiler Developer Guide and Reference](https://www.intel.com/content/dam/develop/external/us/en/documents/oneapi_fortran_compiler.pdf):

```auto
$ rga -A4 "advance='no'.{1,20}end-of-record"
oneapi_fortran_compiler.pdf
For formatted input using nonadvancing I/O (ADVANCE='NO'), an end-of-record (EOR) condition is
returned. If the file was opened with PAD='YES', additional fields are read as spaces.
For list-directed input, another record is read.
For NAMELIST input, another record is read.
For unformatted input, an error occurs.

```

---

<div class="post-metadata">

**Author:** ![Hongyi](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hongyi](https://fortran-lang.discourse.group/u/Hongyi)\
**Post date:** [April 4, 2022, 8:26am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/5 "2022-04-04T08:26:56Z")

</div>

Another problem is that the code always returns error results for large integers, as follows:

 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/1/1a92f68ae95d883c3cc5a6ccb25f056a7f3a2814.png)

Any hints for fixing this problem?

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![Carltoffel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/carltoffel/32/1680_2.png) [@Carltoffel](https://fortran-lang.discourse.group/u/Carltoffel)\
**Post date:** [April 4, 2022, 8:47am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/6 "2022-04-04T08:47:25Z")

</div>

50! is ~ 3.04E+064  
It doesn’t fit into an integer with a range of 18 decimal digits.  
Even a 128 bit integer (range = 38) cannot store such a huge number.  
A 64 bit real is needed here.

---

<div class="post-metadata">

**Author:** ![Sideboard](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sideboard/32/779_2.png) [@Sideboard](https://fortran-lang.discourse.group/u/Sideboard)\
**Post date:** [April 4, 2022, 9:31am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/7 "2022-04-04T09:31:04Z")

</div>

And then you may want to use the intrinsic [gamma function](https://fortranwiki.org/fortran/show/gamma) instead.

---

<div class="post-metadata">

**Author:** ![Hongyi](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hongyi](https://fortran-lang.discourse.group/u/Hongyi)\
**Post date:** [April 4, 2022, 9:41am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/8 "2022-04-04T09:41:12Z")

</div>

I tried but still failed:

 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/9/9002f420cbdee5246453b4799db8c3589c9427c9.png)

---

<div class="post-metadata">

**Author:** ![Hongyi](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hongyi](https://fortran-lang.discourse.group/u/Hongyi)\
**Post date:** [April 4, 2022, 10:00am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/9 "2022-04-04T10:00:49Z")

</div>

> [@urbanjost](#):
>
> ```auto
> write(*,'(a)',advance='no')'Number? : '
> 
> ```

Another question: What’s the meaning of `'(a)'` used above?

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![Sideboard](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sideboard/32/779_2.png) [@Sideboard](https://fortran-lang.discourse.group/u/Sideboard)\
**Post date:** [April 4, 2022, 10:07am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/10 "2022-04-04T10:07:24Z")

</div>

That’s the formatting string, which describes what you will provide after the parenthesis. In this case you tell it there will be a single string (`a`).

---

<div class="post-metadata">

**Author:** ![Carltoffel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/carltoffel/32/1680_2.png) [@Carltoffel](https://fortran-lang.discourse.group/u/Carltoffel)\
**Post date:** [April 4, 2022, 10:14am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/11 "2022-04-04T10:14:21Z")

</div>

Have a look at [Fortran - Basic Input Output](https://www.tutorialspoint.com/fortran/fortran_basic_input_output.htm) for further information.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [April 4, 2022, 10:15am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/12 "2022-04-04T10:15:08Z")

</div>

> [@Hongyi](#):
>
> I tried but still failed:

It’s because your default reals are 32 bits.  
You need:

```auto
use iso_fortran_env, only: real64
real(real64) :: x 

```

---

<div class="post-metadata">

**Author:** ![Hongyi](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hongyi](https://fortran-lang.discourse.group/u/Hongyi)\
**Post date:** [April 4, 2022, 10:23am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/13 "2022-04-04T10:23:55Z")

</div>

I also figured it out with the following query:

```auto
$ rga -I -A1 -U "^[]*A\n.{1,20}character"| awk '!a[$0]++'
A

A[w]
Transfers character or Hollerith
values.

```

But what’s the `Hollerith values` as mentioned above?

---

<div class="post-metadata">

**Author:** ![Hongyi](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hongyi](https://fortran-lang.discourse.group/u/Hongyi)\
**Post date:** [April 4, 2022, 10:30am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/14 "2022-04-04T10:30:08Z")

</div>

> [@vmagnin](#):
>
> ```auto
> use iso_fortran_env, only: real64
> real(real64) :: x 
> 
> ```

Yes. The following code snippet works:

```fortran
program test_gamma

    use iso_fortran_env, only: real64
    real(real64) :: x

    write(*,'(a)',advance='no') 'Number: '
    read(*,*) x
    write(*,*) 'Factorial is ', gamma(x+1)
end program test_gamma

```

For more information, see the following screenshot:

 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/3/3db9358a4cc142a0d4a0afafc6d4873d5673723c.png)

But as you can see, the result is displayed in scientific notation, but I want to show full integer digits. How to achieve this goal?

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [April 4, 2022, 10:34am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/15 "2022-04-04T10:34:25Z")

</div>

> [@Hongyi](#):
>
> but I want to show full integer digits. How to achieve this goal?

You can’t, because it’s a floating point 64 bits real: you will have at max 16 digits.  
If you want to compute big factorials, you will need a Fortran library dealing with arbitrary integer size. Python is adapting the number of bytes needed to code an integer, Fortran is using fixed size integers (generally 8, 16, 32, 64 bits).

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [April 4, 2022, 10:58am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/16 "2022-04-04T10:58:10Z")

</div>

> [@vmagnin](#):
>
> > [@Hongyi](#):
> >
> > but I want to show full integer digits. How to achieve this goal?
> 
> You can’t, because it’s a floating point 64 bits real: you will have at max 16 digits.  
> If you want to compute big factorials, you will need a Fortran library dealing with arbitrary integer size.

Such as [MPFUN2020](https://github.com/jacobwilliams/MPFUN2020) by David H. Bailey or [FM](http://dmsmith.lmu.build/) by David M. Smith.

---

<div class="post-metadata">

**Author:** ![Carltoffel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/carltoffel/32/1680_2.png) [@Carltoffel](https://fortran-lang.discourse.group/u/Carltoffel)\
**Post date:** [April 4, 2022, 11:03am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/17 "2022-04-04T11:03:48Z")

</div>

Since `gamma(x) = (x-1)!`, you need to add one to x:

```auto
write(*,*) 'Factorial is ', gamma(x+1)

```

---

<div class="post-metadata">

**Author:** ![Hongyi](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hongyi](https://fortran-lang.discourse.group/u/Hongyi)\
**Post date:** [April 4, 2022, 1:30pm UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/18 "2022-04-04T13:30:37Z")

</div>

> [@Carltoffel](#):
>
> `write(*,*) 'Factorial is ', gamma(x+1)`

Thank you for pointing out my mistake. I have made relevant correction.

---

<div class="post-metadata">

**Author:** ![Hongyi](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hongyi](https://fortran-lang.discourse.group/u/Hongyi)\
**Post date:** [April 4, 2022, 1:35pm UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/19 "2022-04-04T13:35:59Z")

</div>

> [@Beliavsky](#):
>
> Such as [MPFUN2020](https://github.com/jacobwilliams/MPFUN2020) by David H. Bailey or [FM](http://dmsmith.lmu.build/) by David M. Smith.

Thank you for letting me know about these tools/packages/libraries.

---

<div class="post-metadata">

**Author:** ![gak](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gak/32/146_2.png) [@gak](https://fortran-lang.discourse.group/u/gak)\
**Post date:** [April 8, 2022, 2:00am UTC](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124/20 "2022-04-08T02:00:54Z")

</div>

Bock in the days before CHARACTER type in Fortran, you could initialize data values with Hollerith values. It was a way of saying “The next n characters should be treated like data”. The `16H` references below says use the next 16 characters to fill x(1) and x(2). It was about the only way to create character data in 1966. It was also allowed in `FORMAT` statements to label output.

```auto
      double complex x(2) 
      data x /16Habcdefghijklmnop, 16Hqrstuvwxyz012345/ 
      write( 6, '(4A8, "!")' ) x 
      end

```

[Next page](https://fortran-lang.discourse.group/t/the-strange-syntax-of-write-a-used-in-the-calculation-of-the-factorial/3124.md?page=2)
