きったんの頭

Factorial

n! = n * (n - 1) * … * 2 * 1   | JS | Java |

| Page1.xaml.cs | Page1.xaml |
// Page1.xaml.cs
// https://mind.kittttttan.info/cs/fact
using System;
using System.Windows;
using System.Windows.Controls;
using System.Numerics;
using System.Diagnostics;

namespace WpfBrowserApplication1
{
    /// <summary>
    /// Page1.xaml の相互作用ロジック
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int n;
            try
            {
                n = int.Parse(this.textBox1.Text);
            }
            catch (Exception)
            {
                this.textBox2.Text = "Error";
                return;
            }

            Stopwatch sw = new Stopwatch();
            sw.Start();

            BigInteger a = 1;
            for (int i = 2; i < n + 1; i++)
            {
                a *= i;
            }

            this.textBox2.Text = n.ToString() +"! =\n"+ a.ToString();

            sw.Stop();
            this.label1.Content = sw.ElapsedMilliseconds.ToString() + "ms";
        }
    }
}
<!-- Page1.xaml -->
<Page x:Class="WpfBrowserApplication1.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" Title="Page1" Height="124" Width="300">
    <Grid Height="121">
        <TextBox Height="24" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="777" DataContext="{Binding}" MaxLength="10" />
        <Button Content="OK" Height="23" HorizontalAlignment="Left" Margin="138,12,0,0" Name="button1" VerticalAlignment="Top" Width="40" Click="button1_Click" />
        <Label Content="0ms" Height="28" HorizontalAlignment="Right" Margin="0,10,12,0" Name="label1" VerticalAlignment="Top" Width="104" HorizontalContentAlignment="Right" />
        <TextBox Height="67" HorizontalAlignment="Left" Margin="12,42,0,0" Name="textBox2" VerticalAlignment="Top" Width="276" IsReadOnly="True" TextWrapping="Wrap" IsReadOnlyCaretVisible="True" VerticalScrollBarVisibility="Visible" AllowDrop="False" HorizontalScrollBarVisibility="Hidden" IsUndoEnabled="False" />
    </Grid>
</Page>