The sequence of triangle numbers is generated by adding the natural numbers. So the 7^(th) triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int a = 0; int b = 0; int c = 0; for (int n = 2; a + b + c != 1000; n ++) { for (int m = 1; m < n; m ++) { //nとmが互いに素かどうかを確かめる bool b_coprime = true; if ((int)(n / 2) * 2 == n && (int)(m / 2) * 2 == m) { b_coprime = false; } else { int p = m; if ((int)(m / 2) * 2 == m) { for (int q = m; (int)(p / 2) * 2 == p;) { p /= 2; } } for (int q = 3; q <= p; q += 2) { if ((int)(p/q)*q==p&&(int)(n/q)*q==n) { b_coprime = false; } } } /*確認 if (b_coprime == true) { Console.WriteLine(n.ToString() + "," + m.ToString() + ":" + b_coprime); Console.ReadLine(); }*/
//mとnが同時に偶数、奇数である場合は計算しない bool b_nm_oe = false; if ((int)(n / 2) * 2 == n && (int)(m / 2) * 2 == m) { b_nm_oe = true; } if ((int)(n / 2) * 2 != n && (int)(m / 2) * 2 != m) { b_nm_oe = true; }
if (b_coprime == true && b_nm_oe == false) { a = n * n - m * m; b = 2 * n * m; c = n * n + m * m; Console.WriteLine(a.ToString() + "," + b.ToString() + "," + c.ToString() + ":" + (a + b + c).ToString()); Console.ReadLine(); } } } Console.WriteLine(a * b * c); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int a = 0; int b = 0; int c = 0; int req = 1000; bool ans = false; for (int i = 1; i <= req; i++) { for (int j = i + 1; j <= req; j++) { int k = req - i - j; if (i * i + j * j == k * k) { if (i + j + k == req) { a = i; b = j; c = k; ans = true; } break; } if (j > k) { break; } } } Console.WriteLine(a.ToString()+","+b.ToString()+","+c.ToString()); Console.WriteLine((a*b*c).ToString()); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; /* 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? */ namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int a = 20;
//2-20までの素数表作成 int yousosu = 2; bool b_sosu = true; int[] sosu = new int[2]; sosu[0] = 2; sosu[1] = 3; for (int i = 5; i <= a; i += 2) { int k = 0; for (int j = 1; j <= yousosu - 1; j++) { if ((int)(i / sosu[j]) * sosu[j] == i) { b_sosu = false; } k = j; } if (b_sosu != false) { for (int j = sosu[k]; j <= Math.Sqrt(a); j += 2) { if ((int)(i / j) * j == i) { b_sosu = false; } } } if (b_sosu == true) { sosu.CopyTo(sosu = new int[yousosu+1],0); sosu[yousosu] = i; yousosu++; } else { b_sosu = true; } }
/*きちんと素数配列が出来ているかの✔ for (int i = 0; i <= yousosu -1; i++) { Console.WriteLine(sosu[i]); } Console.ReadLine(); */
//最小公倍数を求める int[,] yakusu = new int[21,21]; for (int i = 2; i <= a; i++) { int b = i; for (int j = 0; j <= yousosu - 1; j++) { for (int k = 0; k >= 0; k = k) { if ((int)(b / sosu[j]) * sosu[j] == b) { yakusu[i, j]++; b = b / sosu[j]; } else { break; } } if (b == 1) { break; }
} } /*きちんと分配出来てるか確認 for (int i = 2; i <= a; i++) { Console.Write(i.ToString() + " : "); for (int j = 0; j <= yousosu - 1; j++) { Console.Write(yakusu[i, j].ToString() + ","); } Console.Write("\n"); } Console.ReadLine();*/
int[] max = new int[yousosu]; for (int i = 0; i <= yousosu - 1; i++) { for (int j = 2; j <= a; j++) { if (yakusu[j, i] >= max[i]) { max[i] = yakusu[j, i]; } } } /*確認*/ int kekka = 1; for (int i = 0; i <= yousosu-1; i++) { kekka = kekka * (int)Math.Pow(sosu[i],max[i]); } Console.WriteLine(kekka); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text;
/* Problem 4 16 November 2001
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers. */ namespace ConsoleApplication1 { class Program { static void Main(string[] args) { double a = 999; double b = 0; double max = 0; string maxstr = ""; for (double i = 0; i <= a; i++) { for (double j = 0; j <= i; j++) { b = (a - i) * (a - j);
//回文数か判断する処理 bool kaibun = true; double c = b; int[] keta = new int[b.ToString().Length+1]; for (int k = 1; k <= b.ToString().Length; k++) { keta[k] = int.Parse(b.ToString().Substring(k-1, 1)); }
if ((int)(b.ToString().Length / 2) * 2 == b.ToString().Length) { for (int l = 1; l <= b.ToString().Length / 2; l++) { if (keta[l] != keta[b.ToString().Length - l + 1]) { kaibun = false; break; } } } else { for (int l = 1; l <= (int)(b.ToString().Length / 2); l++) { if (keta[l] != keta[b.ToString().Length - l + 1]) { kaibun = false; break; } } }
if (kaibun == true) { if (b >= max) { max = b; maxstr = b.ToString() + " = " + (a - i).ToString() + " * " + (a - j).ToString(); } } } } Console.WriteLine(maxstr); Console.ReadLine(); } } }
a = 600851475143 l = 0 repeat sqrt(a) - 1 b = cnt + 2 if int(a/b)*b == a { a = a/b l = b if a == 1 : break } loop
input l
出力結果:・・・・0 HSPの変数型では600851475143はあまりにも大きすぎるみたいです。
困りましたね・・・ じゃあ、C#で解くことにしましょう!
/* Problem 3 02 November 2001
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ? */
using System; namespace p { class kaiho { static void Main() { double a = 600851475143; double b = a; double l = 0;
//Console.WriteLine(Math.Sqrt(a)); for (double i = 2; i < Math.Ceiling(Math.Sqrt(b)); i++) { if (Math.Floor(a / i) == Math.Ceiling(a / i)) { a = a / i; if (a == 1) { l = i; break; } } } Console.WriteLine(l); Console.ReadKey(); } } }
出力結果:6857
for (double i = 2; i < Math.Ceiling(Math.Sqrt(b)); i++) これは素数判定でよく使うテクニックですね。ある数の素因数は、その平方根より大きくなることはないという性質を利用したものです。今回の解答には必要ありませんが、一応テクニックとして紹介しておきます。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
この酒は米を使った酒類では最も素朴な形態の物と言われ、一般の酒店でも購入可能な濁り酒に近い。これを沈殿濾過する事で清酒を作る事も可能だが、清酒になる程には漉さずに飲用する。清酒に比べ濾過が不十分であるため、未発酵の米に含まれる澱粉や、澱粉が分解した糖により、ほんのり甘い風味であるが、アルコール度は清酒と同程度の14~17度にもなるため、口当たりのよさがあだとなってつい飲み過ごして悪酔いしやすい。 wikipedia