詹姆斯·乔伊斯(James Joyce)的“尤利西斯”(Ulysses)序列表示N NN中的位数。
詹姆斯·乔伊斯(James Joyce)的《尤利西斯》(Ulysses)序列中的前几个词是
1, 2, 13, 155, 2185, 36306……..
给定整数N ,任务是打印詹姆斯·乔伊斯(James Joyce)的“尤利西斯”(Ulysses)序列的第N个项。
例子:
Input: 2
Output: 2
Explanation:
Number of digits in 222 is 2 since 222 = 16
Input: 3
Output: 13
方法: Num中的位数由下式给出: 。因此,N NN中的位数由下式给出:
要查看乔伊斯的所有数字,我们需要打印大约3.7亿个数字。假设每行100位和每页100行,这意味着需要写下约37卷(每1000页) 明确地。乔伊斯在《尤利西斯》中的估计为33卷;考虑到他在数学上的糟糕表现,还不错。
下面是上述方法的实现:
C++
// C++ implementation of
// the above approach
#include
using namespace std;
// Function to return N-th term
// of James Joyce's "Ulysses" sequence.
int nthTerm(int n)
{
return floor(pow(n, n) * log10(n)) + 1;
}
// Driver Code
int main()
{
int n = 3;
cout << nthTerm(n);
return 0;
}
Java
// Java implementation of
// the above approach
class GFG{
// Function to return N-th term
// of James Joyce's "Ulysses" sequence.
static int nthTerm(int n)
{
return (int)(Math.floor(Math.pow(n, n) *
Math.log10(n)) + 1);
}
// Driver Code
public static void main(String[] args)
{
int n = 3;
System.out.print(nthTerm(n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the
# above approach
import math
# Function to return N-th term
# of James Joyce's "Ulysses" sequence.
def nthTerm(n):
return (math.floor(math.pow(n, n) *
math.log10(n)) + 1);
# Driver Code
if __name__ == "__main__" :
# Given number
n = 3;
# Function call
print(nthTerm(n));
# This code is contributed by rock_cool
C#
// C# implementation of
// the above approach
using System;
class GFG{
// Function to return N-th term
// of James Joyce's "Ulysses" sequence.
static int nthTerm(int n)
{
return (int)(Math.Floor(Math.Pow(n, n) *
Math.Log10(n)) + 1);
}
// Driver Code
public static void Main()
{
int n = 3;
Console.Write(nthTerm(n));
}
}
// This code is contributed by Nidhi_biet
Javascript
输出:
13
参考文献: https : //oeis.org/A054382