Skip to content

Instantly share code, notes, and snippets.

View gushwell's full-sized avatar

gushwell gushwell

View GitHub Profile
@gushwell
gushwell / gcd.cs
Last active May 20, 2018 07:19
C#:最大公約数を求める (ユークリッドの互除法) ref: https://qiita.com/gushwell/items/e9614b4ac2bea3fc6486
class Program {
static void Main(string[] args) {
Console.WriteLine(Gcd(0, 8));
Console.WriteLine(Gcd(12, 8));
Console.WriteLine(Gcd(3400, 6596));
Console.WriteLine(Gcd(12707, 12319));
}
// ユークリッドの互除法
public static int Gcd(int a, int b) {
if (a < b)
@gushwell
gushwell / FiboRecursive.cs
Last active May 20, 2018 07:16
C#:フィボナッチ数列を列挙する ref: https://qiita.com/gushwell/items/dc3731a6131a3bdba406
public static long Fibo(int n) {
if (n == 0)
return 0;
if (n == 1)
return 1;
return Fibo(n - 1) + Fibo(n - 2);
}
using System;
using System.Linq;
namespace Gushwell.Etude {
class Program {
static void Main(string[] args) {
// 1 - 100 までのメビウス関数を求める
int upper = 100;
Mebius mebius = new Mebius(upper);
for (int i = 1; i <= upper; i++) {
@gushwell
gushwell / PrimesBest.cs
Last active February 12, 2018 04:25
C#:エラトステネスの篩(ふるい)再び ref: https://qiita.com/gushwell/items/6370e32371e983358519
// 下限、上限が指定できるbool型配列
class BoundedBoolArray {
private BitArray _array;
private int _lower;
public BoundedBoolArray(int lower, int upper) {
_array = new BitArray(upper - lower + 1);
_lower = lower;
}
@gushwell
gushwell / PrimesSavingMemory.cs
Last active April 14, 2018 04:48
C#:エラトステネスの篩を使って素数を求める ref: https://qiita.com/gushwell/items/5d3988a2f5de0587c88f
static IEnumerable<int> Primes(int maxnum) {
// var sieve = new BitArray(maxnum + 1, true);
bool[] sieve = Enumerable.Repeat(true, maxnum + 1).ToArray();
int squareroot = (int)Math.Sqrt(maxnum);
for (int i = 2; i <= squareroot; i++) {
if (sieve[i] == false)
continue;
for (int n = i * 2; n <= maxnum; n += i)
sieve[n] = false;
}
@gushwell
gushwell / Factorial.cs
Last active April 14, 2018 04:53
C#で階乗の計算(再帰処理) ref: https://qiita.com/gushwell/items/51abfaa8bd05a38a5dd0
public static long Factorial(int n) {
if (n == 0)
return 1L;
return n * Factorial(n - 1);
}
using System;
using System.Windows;
using System.Windows.Media.Imaging;
namespace DataTemplateSample {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
MyImage mi = new MyImage();