Open In App

Find N values of X1, X2, … Xn such that X1 < X2 < … < XN and sin(X1) < sin(X2) < … < sin(XN)

Last Updated : 01 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to find the N integer values of Xi such that X1 < X2 < … < XN and sin(X1) < sin(X2) < … < sin(XN).
Examples: 
 

Input: N = 5 
Output: 
X1 = 0 sin(X1) = 0.000000 
X2 = 710 sin(X2) = 0.000060 
X3 = 1420 sin(X3) = 0.000121 
X4 = 2130 sin(X4) = 0.000181 
X5 = 2840 sin(X5) = 0.000241
Input: N = 3 
Output: 
X1 = 0 sin(X1) = 0.000000 
X2 = 710 sin(X2) = 0.000060 
X3 = 1420 sin(X3) = 0.000121 
 

 

Approach: The idea is to use the fractional value of PI(&PI); i.e., Pi = 355/113 as it given best rational value of PI of accuracy being 0.000009%. 
 

As,
PI = 355/113
=> 113*PI = 355
=> 2*(113*PI) = 710

As sin() function has a period of 2*PI,
Therefore sin(2*k*PI + Y) = sin(Y);

As per the above equation to get the value X1 < X2 < … < XN and sin(X1) < sin(X2) < … < sin(XN) we must find the value of sin(X) with an increment of 710.
Below is the implementation of the above approach: 
 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print all such Xi s.t.
// all Xi and sin(Xi) are strictly
// increasing
void printSinX(int N)
{
    int Xi = 0;
    int num = 1;
 
    // Till N becomes zero
    while (N--) {
 
        cout << "X" << num << " = " << Xi;
        cout << " sin(X" << num << ") = "
             << fixed;
 
        // Find the value of sin() using
        // inbuilt function
        cout << setprecision(6)
             << sin(Xi) << endl;
 
        num += 1;
 
        // increment by 710
        Xi += 710;
    }
}
 
// Driver Code
int main()
{
    int N = 5;
 
    // Function Call
    printSinX(N);
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to print all such Xi s.t.
// all Xi and sin(Xi) are strictly
// increasing
static void printSinX(int N)
{
    int Xi = 0;
    int num = 1;
 
    // Till N becomes zero
    while (N-- > 0)
    {
 
        System.out.print("X" + num + " = " + Xi);
        System.out.print(" sin(X" + num + ") = ");
 
        // Find the value of sin() using
        // inbuilt function
        System.out.printf("%.6f", Math.sin(Xi));
        System.out.println();
        num += 1;
 
        // Increment by 710
        Xi += 710;
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 5;
 
    // Function Call
    printSinX(N);
}
}
 
// This code is contributed by Princi Singh


Python3




# Python3 program for the above approach
import math
 
# Function to print all such Xi s.t.
# all Xi and sin(Xi) are strictly
# increasing
def printSinX(N):
 
    Xi = 0;
    num = 1;
 
    # Till N becomes zero
    while (N > 0):
 
        print("X", num, "=", Xi, end = " ");
        print("sin(X", num, ") =", end = " ");
 
        # Find the value of sin() using
        # inbuilt function
        print("{:.6f}".format(math.sin(Xi)), "\n");
 
        num += 1;
 
        # increment by 710
        Xi += 710;
        N = N - 1;
 
# Driver Code
N = 5;
 
# Function Call
printSinX(N)
 
# This code is contributed by Code_Mech


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to print all such Xi s.t.
// all Xi and sin(Xi) are strictly
// increasing
static void printSinX(int N)
{
    int Xi = 0;
    int num = 1;
 
    // Till N becomes zero
    while (N-- > 0)
    {
        Console.Write("X" + num + " = " + Xi);
        Console.Write(" sin(X" + num + ") = ");
 
        // Find the value of sin() using
        // inbuilt function
        Console.Write("{0:F6}", Math.Sin(Xi));
        Console.WriteLine();
        num += 1;
 
        // Increment by 710
        Xi += 710;
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 5;
 
    // Function Call
    printSinX(N);
}
}
 
// This code is contributed by SoumikMondal


Javascript




<script>
 
// Javascript program for the above approach
  
// Function to print all such Xi s.t.
// all Xi and sin(Xi) are strictly
// increasing
function printSinX(N)
{
    let Xi = 0;
    let num = 1;
   
    // Till N becomes zero
    while (N-- > 0)
    {
   
        document.write("X" + num + " = " + Xi);
        document.write(" sin(X" + num + ") = ");
   
        // Find the value of sin() using
        // inbuilt function
        document.write(Math.sin(Xi).toFixed(6));
        document.write("<br/>");
        num += 1;
   
        // Increment by 710
        Xi += 710;
    }
}
   
 
// Driver Code
     
    let N = 5;
   
    // Function Call
    printSinX(N);
                   
</script>


Output

X1 = 0 sin(X1) = 0.000000
X2 = 710 sin(X2) = 0.000060
X3 = 1420 sin(X3) = 0.000121
X4 = 2130 sin(X4) = 0.000181
X5 = 2840 sin(X5) = 0.000241

Time Complexity: O(N)

Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads