Skip to content

Instantly share code, notes, and snippets.

@taross-f
Created January 6, 2017 10:43
Show Gist options
  • Save taross-f/9abf6c1a3ae60ffd816bbc42f3c52b37 to your computer and use it in GitHub Desktop.
Save taross-f/9abf6c1a3ae60ffd816bbc42f3c52b37 to your computer and use it in GitHub Desktop.
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Net.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Net.Http.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Web.dll</Reference>
<NuGetReference>Microsoft.Net.Http</NuGetReference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<NuGetReference>Sendgrid</NuGetReference>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>Newtonsoft.Json.Bson</Namespace>
<Namespace>Newtonsoft.Json.Converters</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
<Namespace>Newtonsoft.Json.Schema</Namespace>
<Namespace>Newtonsoft.Json.Serialization</Namespace>
<Namespace>SendGrid.SmtpApi</Namespace>
<Namespace>System.IO</Namespace>
<Namespace>System.Net</Namespace>
<Namespace>System.Web.Mail</Namespace>
</Query>
//using System;
//using System.Linq;
//using System.Collections.Generic;
public class PrimeFactorize
{
public static void Main()
{
var n = int.Parse(Console.ReadLine());
var primes = new List<int>();
Do(primes, n, 2);
Console.WriteLine(string.Format("{0}: {1}", n, string.Join(" ", primes.Select(x => x.ToString()).ToArray())));
}
private static void Do(List<int> primes, int input, int denom)
{
// 割る数の二乗が大きくなったらもう終わり
if (denom * denom > input)
{
primes.Add(input);
}
else if (input % denom == 0)
{
primes.Add(denom);
Do(primes, input / denom, denom);
}
else
{
// 偶数排除してチェックを減らす
if (denom == 2)
Do(primes, input, denom + 1);
else
Do(primes, input, denom + 2);
}
}
// yieldでオシャレにやろうとした残骸・・・
// private static IEnumerable<int> CalcPrimes(int input, int denom)
// {
// while (input > denom)
// {
// yield return Calc(ref input, ref denom);
// denom++;
// }
// }
//
// private static int Calc(ref int input, ref int denom)
// {
// if (input == denom)
// {
// return denom;
// }
// else if (input % denom == 0)
// {
// return denom;
// }
// else
// {
// input /= denom;
// return Calc(ref input, ref ++denom);
// }
// }
}
// Define other methods and classes here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment