Skip to content

Commit c8592f2

Browse files
committed
find Nth digit of n
1 parent ee338ac commit c8592f2

10 files changed

+150
-9
lines changed

Math/.vs/Math/v15/.suo

38 KB
Binary file not shown.

Math/Math.Lib/HappyNumber.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010

1111
namespace Math.Lib
1212
{
13-
Write an algorithm to determine if a number is "happy".
13+
// Write an algorithm to determine if a number is "happy".
1414

15-
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
15+
//A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
1616

17-
Example: 19 is a happy number
17+
//Example: 19 is a happy number
1818

19-
1^2 + 9^2 = 82
20-
8^2 + 2^2 = 68
21-
6^2 + 8^2 = 100
22-
1^2 + 0^2 + 0^2 = 1
19+
//1^2 + 9^2 = 82
20+
//8^2 + 2^2 = 68
21+
//6^2 + 8^2 = 100
22+
//1^2 + 0^2 + 0^2 = 1
2323
public class HappyNumberSln {
2424
public bool IsHappy(int n) {
2525
int slow =n, fast = n;

Math/Math.Lib/Math.Lib.csproj

+9
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,19 @@
4040
<Reference Include="System.Xml" />
4141
</ItemGroup>
4242
<ItemGroup>
43+
<Compile Include="AddStringsSln.cs" />
44+
<Compile Include="HappyNumber.cs" />
45+
<Compile Include="MinimumMovesSln.cs" />
4346
<Compile Include="MissingNumberSln.cs" />
47+
<Compile Include="Nthdigit.cs" />
4448
<Compile Include="PerfectNumberSln.cs" />
4549
<Compile Include="PowerOfTwoSln.cs" />
4650
<Compile Include="Properties\AssemblyInfo.cs" />
51+
<Compile Include="ReverseIntegarSln - Copy.cs" />
52+
<Compile Include="ReverseIntegarSln.cs" />
53+
</ItemGroup>
54+
<ItemGroup>
55+
<Content Include="Floyd%27sTortoiseandHare.jpg" />
4756
</ItemGroup>
4857
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
4958
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

Math/Math.Lib/Math.Lib.csproj.user

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ProjectView>ShowAllFiles</ProjectView>
5+
</PropertyGroup>
6+
</Project>

Math/Math.Lib/Nthdigit.cs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Math.Lib
7+
{
8+
// Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
9+
10+
//Note:
11+
//n is positive and will fit within the range of a 32-bit signed integer (n < 231).
12+
13+
//Example 1:
14+
15+
//Input:
16+
//3
17+
18+
//Output:
19+
//3
20+
//Example 2:
21+
22+
//Input:
23+
//11
24+
25+
//Output:
26+
//0
27+
28+
//Explanation:
29+
//The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.
30+
public class Nthdigit
31+
{
32+
//for this issue, there are two different ways to decribe a number
33+
//1 element. this is our common way
34+
//2 Nth digit. this is a new way
35+
public int FindNthDigit(int n)
36+
{
37+
long bas = 9;
38+
int digits = 1, i = 0;
39+
//first: getting n which digit is in
40+
while (n > bas * digits) // prevent overflowing. Since bas is long, so result of bas*digits is auto imporved as long
41+
{
42+
n -= (int)(bas * (digits++)); //nth
43+
i += (int)bas; //number of pasted elements
44+
bas *= 10; //1 digit->9; 2 digits->90; 3 digits->900, ...
45+
}
46+
//second: Nth digit ->element
47+
//in all numbers containing digits, pasted numbers
48+
int pasted = (int)((n - 1) / digits);
49+
int element = pasted + i + 1;
50+
//third: once getting the element Nth digits stands,
51+
//(n-1)%digits of element is solution
52+
int nth = (n - 1) % digits;
53+
return element.ToString()[nth] - '0';
54+
}
55+
}
56+
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* ==============================================================================
2+
* 功能描述:ReverseIntegarSln
3+
* 创 建 者:gz
4+
* 创建日期:2017/5/18 13:05:38
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
//Reverse digits of an integer.
11+
12+
//Example1: x = 123, return 321
13+
//Example2: x = -123, return -321
14+
//Note:
15+
//The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
16+
namespace lt
17+
{
18+
/// <summary>
19+
/// ReverseIntegarSln
20+
/// </summary>
21+
public class ReverseIntegarSln
22+
{
23+
public int Reverse(int x)
24+
{
25+
bool isNegative = x < 0;
26+
long rtn = 0;
27+
while (x != 0)
28+
{
29+
int postBit = x % 10;
30+
long iterval = rtn * 10 + postBit;
31+
if (isNegative && iterval < -2147483648) return 0;
32+
if (iterval > 2147483647) return 0;
33+
rtn = iterval;
34+
x /= 10;
35+
}
36+
return (int)rtn;
37+
}
38+
39+
public int Reverse2(int x)
40+
{
41+
int rtn = 0;
42+
43+
while (x != 0)
44+
{
45+
int postBit = x % 10;
46+
int iterval = rtn * 10 + postBit; //if happens overflow, result != (newResult - post)/10
47+
if ((iterval - postBit) / 10 != rtn) //solve the overflow
48+
{
49+
return 0;
50+
}
51+
rtn = iterval;
52+
x = x / 10;
53+
}
54+
return rtn;
55+
}
56+
}
57+
}

Math/Math.Lib/ReverseIntegarSln.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//Example2: x = -123, return -321
1414
//Note:
1515
//The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
16-
namespace lt
16+
namespace Math.Lib
1717
{
1818
/// <summary>
1919
/// ReverseIntegarSln

Math/Math.v12.suo

55.5 KB
Binary file not shown.

Math/Math/Math.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
<Compile Include="Program.cs" />
4747
<Compile Include="Properties\AssemblyInfo.cs" />
4848
</ItemGroup>
49+
<ItemGroup>
50+
<ProjectReference Include="..\Math.Lib\Math.Lib.csproj">
51+
<Project>{8b0938a8-8d54-4ad6-8717-3ec40291a67f}</Project>
52+
<Name>Math.Lib</Name>
53+
</ProjectReference>
54+
</ItemGroup>
4955
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5056
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5157
Other similar extension points exist, see Microsoft.Common.targets.

Math/Math/Program.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Math.Lib;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -9,6 +10,12 @@ class Program
910
{
1011
static void Main(string[] args)
1112
{
13+
Nthdigit nth = new Nthdigit();
14+
if(Int32.MaxValue == 2147483647)
15+
{
16+
int n = nth.FindNthDigit(2147483647);
17+
}
18+
1219
}
1320
}
1421
}

0 commit comments

Comments
 (0)