Skip to content
- Tutorials
- Python Tutorial
- Taking Input in Python
- Python Operators
- Python Data Types
- Python Loops and Control Flow
- Python Functions
- Python OOPS Concept
- Python Data Structures
- Python Exception Handling
- Python File Handling
- Python Exercises
- Java
- Java Programming Language
- Java Collections
- Java 8 Tutorial
- Java Programs
- Java Interview Questions
- Java Exercises
- Java Quiz
- Java Projects
- Advance Java
- Programming Languages
- System Design
- Interview Corner
- Computer Science Subjects
- DevOps
- Linux
- Software Testing
- Databases
- Android
- Excel
- Mathematics
- Python Tutorial
- DSA
- Data Structures
- Algorithms
- Analysis of Algorithms
- Searching Algorithms
- Sorting Algorithms
- Greedy Algorithms
- Dynamic Programming
- Graph Algorithms
- Pattern Searching
- Recursion
- Backtracking
- Divide and Conquer
- Mathematical Algorithms
- Geometric Algorithms
- Bitwise Algorithms
- Randomized Algorithms
- Branch and Bound
- Algorithms Tutorial
- DSA Tutorial
- Practice
- All DSA Problems
- Problem of the Day
- Company Wise Coding Practice
- GfG SDE Sheet
- Practice Problems Difficulty Wise
- Language Wise Coding Practice
- Curated DSA Lists
- Competitive Programming
- Company Wise SDE Sheets
- DSA Cheat Sheets
- Top Interview Questions
- Puzzles
- Data Science
- Web Tech
- Courses
-
- DSA Course
- DSA
- Practice Mathematical Algorithm
- Mathematical Algorithms
- Pythagorean Triplet
- Fibonacci Number
- Euclidean Algorithm
- LCM of Array
- GCD of Array
- Binomial Coefficient
- Catalan Numbers
- Sieve of Eratosthenes
- Euler Totient Function
- Modular Exponentiation
- Modular Multiplicative Inverse
- Stein's Algorithm
- Juggler Sequence
- Chinese Remainder Theorem
- Quiz on Fibonacci Numbers
Last Updated : 14 Aug, 2024
Summarize
Comments
Improve
Prime factor is the factor of the given number which is a prime number. Factors are the numbers you multiply together to get another number. In simple words, prime factor is finding which prime numbers multiply together to make the original number.
Example: The prime factors of 15 are 3 and 5 (because 3×5=15, and 3 and 5 are prime numbers).
Some interesting fact about Prime Factor :- There is only one (unique!) set of prime factors for any number.
- In order to maintain this property of unique prime factorizations, it is necessary that the number one, 1, be categorized as neither prime nor composite.
- Prime factorizations can help us with divisibility, simplifying fractions, and finding common denominators for fractions.
- Pollard’s Rho is a prime factorization algorithm, particularly fast for a large composite number with small prime factors.
- Cryptography is the study of secret codes. Prime Factorization is very important to people who try to make (or break) secret codes based on numbers.
How to print a prime factor of a number?
Naive solution:
Given a number n, write a function to print all prime factors of n. For example, if the input number is 12, then output should be “2 2 3” and if the input number is 315, then output should be “3 3 5 7”.Following are the steps to find all prime factors:
- While n is divisible by 2, print 2 and divide n by 2.
- After step 1, n must be odd. Now start a loop from i = 3 to square root of n. While i divides n, print i and divide n by i, increment i by 2 and continue.
- If n is a prime number and is greater than 2, then n will not become 1 by above two steps. So print n if it is greater than 2.
C++ #include <iostream>#include <cmath>// A function to print all prime factors of a given number n void primeFactors(int n) { // Print the number of 2s that divide n while (n % 2 == 0) { std::cout << 2 << " "; n = n / 2; } // n must be odd at this point. So we can skip // one element (Note i = i + 2) for (int i = 3; i <= std::sqrt(n); i = i + 2) { // While i divides n, print i and divide n while (n % i == 0) { std::cout << i << " "; n = n / i; } } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) std::cout << n << " "; } /* Driver program to test above function */int main() { int n = 315; primeFactors(n); return 0; }
Java // Program to print all prime factorsimport java.io.*;import java.lang.Math;class GFG { // A function to print all prime factors // of a given number n public static void primeFactors(int n) { // Print the number of 2s that divide n while (n % 2 == 0) { System.out.print(2 + " "); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i += 2) { // While i divides n, print i and divide n while (n % i == 0) { System.out.print(i + " "); n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) System.out.print(n); } public static void main(String[] args) { int n = 315; primeFactors(n); }}
Python # Python program to print prime factorsimport math# A function to print all prime factors of # a given number ndef primeFactors(n): # Print the number of two's that divide n while n % 2 == 0: print 2, n = n / 2 # n must be odd at this point # so a skip of 2 ( i = i + 2) can be used for i in range(3, int(math.sqrt(n))+1, 2): # while i divides n, print i ad divide n while n % i == 0: print i, n = n / i # Condition if n is a prime # number greater than 2 if n > 2: print n # Driver Program to test above functionn = 315primeFactors(n)# This code is contributed by Harsh*t Agrawal
C# // C# Program to print all prime factorsusing System;namespace prime {public class GFG { // A function to print all prime // factors of a given number n public static void primeFactors(int n) { // Print the number of 2s that divide n while (n % 2 == 0) { Console.Write(2 + " "); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.Sqrt(n); i += 2) { // While i divides n, print i and divide n while (n % i == 0) { Console.Write(i + " "); n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) Console.Write(n); } // Driver Code public static void Main() { int n = 315; primeFactors(n); }}}// This code is contributed by Sam007
JavaScript <script>// Javascript program to print all prime factors // A function to print all prime // factors of a given number n function primeFactors(n) { // Print the number of 2s that divide n while (n % 2 == 0) { document.write(2 + " "); n = Math.floor(n/2); } // n must be odd at this point. So we can skip // one element (Note i = i +2) for (let i = 3; i <= Math.sqrt(n); i = i + 2) { // While i divides n, print i and divide n while (n % i == 0) { document.write(i + " "); n = Math.floor(n/i); } } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) document.write(n + " "); } /* Driver code */ let n = 315; primeFactors(n); // This is code is contributed by Mayank Tyagi</script>
PHP <?php// PHP Efficient program to print all// prime factors of a given number// function to print all prime // factors of a given number nfunction primeFactors($n){ // Print the number of // 2s that divide n while($n % 2 == 0) { echo 2, " "; $n = $n / 2; } // n must be odd at this // point. So we can skip // one element (Note i = i +2) for ($i = 3; $i <= sqrt($n); $i = $i + 2) { // While i divides n, // print i and divide n while ($n % $i == 0) { echo $i, " "; $n = $n / $i; } } // This condition is to // handle the case when n // is a prime number greater // than 2 if ($n > 2) echo $n, " ";} // Driver Code $n = 315; primeFactors($n);// This code is contributed by aj_36?>
Output:
3 3 5 7
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
How does this work?See AlsoInteger Factorization Algorithms: A Comparative AnalysisWhat is a Factor? Definition, Examples and Facts,Math Alive Crypto 2Teaching Product of Prime Factors- Steps 1 and 2 take care of composite number and step-3 takes care of prime numbers. To prove that the complete algorithm works, we need to prove that steps 1 and 2 actually take care of composite numbers.
It’s clear that step-1 takes care of even numbers. After step-1, all remaining prime factor must be odd (difference of two prime factors must be at least 2), this explains why i is incremented by 2. - Now the main part is, the loop runs till square root of n. To prove that this optimization works, let us consider the following property of composite numbers.
Every composite number has at least one prime factor less than or equal to square root of itself.
- This property can be proved using counter statement. Let a and b be two factors of n such that a*b = n. If both are greater than √n, then a.b > √n, * √n, which contradicts the expression “a * b = n”.
- In step-2 of the above algorithm, we run a loop and do following-
- Find the least prime factor i (must be less than √n, )
- Remove all occurrences i from n by repeatedly dividing n by i.
- Repeat steps a and b for divided n and i = i + 2. The steps a and b are repeated till n becomes either 1 or a prime number.
Efficient solution:
Programs to find prime factor of a number
- Distinct Prime Factors of Array Product
- N-th prime factor of a given number
- Program to print factors of a number in pairs
- Number of distinct prime factors of first n natural numbers
- Product of unique prime factors of a number
More problems related to Prime Factor
- Count numbers from range whose prime factors are only 2 and 3
- Common prime factors of two numbers
- Least prime factor of numbers till n
- Smallest prime divisor of a number
- Sum of Factors of a Number using Prime Factorization
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Number which has the maximum number of distinct prime factors in the range M to N
Recent Articles on Prime Factor!
Please Login to comment...
Similar Reads
Count numbers in a given range having prime and non-prime digits at prime and non-prime positions respectively
Given two integers L and R, the task is to find the count of numbers in the range [L, R] having prime digits at prime positions and non-prime digits at non-prime positions. Examples: Input: L = 5, R = 22 Output: 7Explanation: The numbers 6, 8, 9, 12, 13, 15, and 17 have prime digits at prime positions and non-prime digits at non-prime positions. In
15 min read
Sum of largest prime factor of each number less than equal to n
Given a non-negative integer n. The problem is to find the sum of the largest prime factor of each number less than equal to n. Examples: Input : n = 10 Output : 32 Largest prime factor of each number Prime factor of 2 = 2 Prime factor of 3 = 3 Prime factor of 4 = 2 Prime factor of 5 = 5 Prime factor of 6 = 3 Prime factor of 7 = 7 Prime factor of 8
8 min read
N-th prime factor of a given number
Given Q queries which consist of two integers, one is number(1 <= number <= 106) and the other is N., the task is to find the N-th prime factor of the given number. Examples: Input: Number of Queries, Q = 4 number = 6, N = 1 number = 210, N = 3 number = 210, N = 2 number = 60, N = 2Output: 2 5 3 2 Explanations: For number = 6, The prime facto
15 min read
Numbers with sum of digits equal to the sum of digits of its all prime factor
Given a range, the task is to find the count of the numbers in the given range such that the sum of its digit is equal to the sum of all its prime factors digits sum.Examples: Input: l = 2, r = 10 Output: 5 2, 3, 4, 5 and 7 are such numbers Input: l = 15, r = 22 Output: 3 17, 19 and 22 are such numbers As, 17 and 19 are already prime. Prime Factors
13 min read
Count all the numbers less than 10^6 whose minimum prime factor is N
Given a number N which is prime. The task is to find all the numbers less than or equal to 10^6 whose minimum prime factor is N.Examples: Input: N = 2 Output: 500000 Input: N = 3 Output: 166667 Approach: Use sieve of Eratosthenes to find the solution to the problem. Store all the prime numbers less than 10^6 . Form another sieve that will store the
7 min read
Find sum of a number and its maximum prime factor
Given an integer N, the task is to find the sum of N and it's maximum prime factor.Examples: Input: 19 Output: 38 Maximum prime factor of 19 is 19. Hence, 19 + 19 = 38Input: 8 Output: 10 8 + 2 = 10 Approach: Find the largest prime factor of the number and store it in maxPrimeFact then print the value of N + maxPrimeFact.Below is the implementation
6 min read
Sum of Maximum and Minimum prime factor of every number in the Array
Given an array arr[], the task is to find the sum of the maximum and the minimum prime factor of every number in the given array.Examples: Input: arr[] = {15} Output: 8 The maximum and the minimum prime factors of 15 are 5 and 3 respectively.Input: arr[] = {5, 10, 15, 20, 25, 30} Output: 10 7 8 7 10 7 Approach: The idea is to use Sieve of Eratosthe
9 min read
Count of distinct power of prime factor of N
Given a positive integer N, the task is to find the total number of distinct power of prime factor of the given number N.Examples: Input: N = 216 Output: 4 Explanation: 216 can be expressed as 2 * 22 * 3 * 32. The factors satisfying the conditions are 2, 22, 3 and 32 as all of them are written as distinct positive powers of prime factors.Input: N =
6 min read
Minimize steps required to make two values equal by repeated division by any of their prime factor which is less than M
Given three positive integers M, X, and Y, the task is to find the minimum number of operations required to make X and Y equal such that in each operation divide X or Y by one of its prime factor less than M. If it is not possible to make X and Y equal, then print "-1". Examples: Input: X = 20, Y =16, M = 10Output: 3Explanation:Perform the operatio
11 min read
Count of numbers up to N having at least one prime factor common with N
Given an integer N, the task is to count the number of integers from the range [1, N] having at least one in common prime factor with N other than 1. Examples: Input: N = 5Output: 1Explanation: Since 5 is prime. Therefore, there is no other number which is at most N, that has at least one common factor with N except N itself. Therefore, the count i
10 min read
Largest integer upto N having greatest prime factor greater than its square root
Given a positive integer N, the task is to find the largest number in the range [1, N] such that the square root of the number is less than its greatest prime factor. Input: N = 15Output: 15Explanation: The prime factors of 15 are {3, 5}. The square root of 15 is 3.87 (i.e, 3.87 < 5). Therefore 15 is the largest valid integer in the given range.
7 min read
Smallest integer having at least K prime divisors with difference between each factor at least D
Given two integers D and K. The task is to find the smallest number N which has at least K prime divisors and the difference between each pair of divisors is at least D. Examples Input: D = 3, K = 2Output: 55Explanation: It is smallest number which has 4 divisors 1 and 2 prime divisors 5, 11 and their difference between any of the pair is D. Input:
7 min read
Least prime factor of numbers till n
Given a number n, print least prime factors of all numbers from 1 to n. The least prime factor of an integer n is the smallest prime number that divides the number. The least prime factor of all even numbers is 2. A prime number is its own least prime factor (as well as its own greatest prime factor).Note: We need to print 1 for 1.Example : Input :
8 min read
Exactly n distinct prime factor numbers from a to b
You are given two numbers a and b (1 <= a,b <= 10^8 ) and n. The task is to find all numbers between a and b inclusively having exactly n distinct prime factors. The solution should be designed in a way that it efficiently handles multiple queries for different values of a and b like in Competitive Programming.Examples: Input : a = 1, b = 10,
15+ min read
k-th prime factor of a given number
Given two numbers n and k, print k-th prime factor among all prime factors of n. For example, if the input number is 15 and k is 2, then output should be "5". And if the k is 3, then output should be "-1" (there are less than k prime factors). Examples: Input : n = 225, k = 2 Output : 3 Prime factors are 3 3 5 5. Second prime factor is 3. Input : n
15+ min read
Queries on the sum of prime factor counts in a range
There are Q queries. Each query is of the form of L and R. The task is to output sum of number of prime factors of each number in the given range of each query.Examples: Input : Q = 2 L = 6, R = 10 L = 1, R = 5 Output : 7 4 For query 1, 6 => 2 [Prime factors are 2 and 3] 7 => 1 8 => 1 9 => 1 10 => 2 Sum = 2 + 1 + 1 + 1 + 2 = 7 For qu
7 min read
Nearest element with at-least one common prime factor
Given an array arr[], find the nearest element for every element such that there is at least one common prime factor. In the output, we need to print the position of the closest element.Example: Input: arr[] = {2, 9, 4, 3, 13} Output: 3 4 1 2 -1 Explanation : Closest element for 1st element is 3rd. =>Common prime factor of 1st and 3rd elements i
15+ min read
Count of subarrays whose products don't have any repeating prime factor
Given an array of integers. Find the total number of subarrays whose product of all elements doesn't contain repeating prime factor in prime decomposition of resulting number. Examples: Input: 2 3 9 Output: 3 Explanation: Total sub-array are:- {2}, {3}, {9}, {2, 3}, {3, 9}, {2, 3, 9} Subarray which violets the property are:- {9} -> {3 * 3}, Sinc
13 min read
Find largest prime factor of a number
Given a positive integer 'n'( 1 <= n <= 1015). Find the largest prime factor of a number. Input: 6 Output: 3 Explanation Prime factor are 2 and 3. Largest of them is '3' Input: 15 Output: 5The approach is simple, just factorize the given number by dividing it with the divisor of a number and keep updating the maximum prime factor. See this to
11 min read
Prime numbers after prime P with sum S
Given three numbers sum S, prime P, and N, find all N prime numbers after prime P such that their sum is equal to S.Examples : Input : N = 2, P = 7, S = 28 Output : 11 17 Explanation : 11 and 17 are primes after prime 7 and (11 + 17 = 28) Input : N = 3, P = 2, S = 23 Output : 3 7 13 5 7 11 Explanation : 3, 5, 7, 11 and 13 are primes after prime 2.
13 min read
Sum of each element raised to (prime-1) % prime
Given an array arr[] and a positive integer P where P is prime and none of the elements of array are divisible by P. Find sum of all the elements of the array raised to the power P - 1 i.e. arr[0]P - 1 + arr[1]P - 1 + ... + arr[n - 1]P - 1 and print the result modulo P.Examples: Input: arr[] = {2, 5}, P = 3 Output: 2 22 + 52 = 29 and 29 % 3 = 2Inpu
3 min read
Print the nearest prime number formed by adding prime numbers to N
Given a number N. The task is to print the nearest prime if the number is not prime by making it prime by adding prime numbers sequentially from 2. Examples: Input: N = 8 Output: 13 8 is not prime, so add the first prime to it to get 10 10 is not prime, hence add the second prime, i.e., 3 to get 13 which is prime. Input: N = 45 Output: 47 Naive App
11 min read
Maximize difference between sum of prime and non-prime array elements by left shifting of digits minimum number of times
Given an array arr[] of size N, the task is to find the maximum difference between the sum of the prime numbers and the sum of the non-prime numbers present in the array, by left shifting the digits of array elements by 1 minimum number of times. Examples: Input: arr[] = {541, 763, 321, 716, 143}Output: Difference = 631, Count of operations = 6Expl
14 min read
Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
Given an array of positive numbers, the task is to calculate the absolute difference between sum of non-prime numbers and prime numbers. Note: 1 is neither prime nor non-prime. Examples: Input : 1 3 5 10 15 7 Output : 10 Explanation: Sum of non-primes = 25 Sum of primes = 15 Input : 3 4 6 7 Output : 0 Naive Approach: A simple solution is to travers
15+ min read
Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
Given an array of positive numbers, the task is to calculate the absolute difference between product of non-prime numbers and prime numbers.Note: 1 is neither prime nor non-prime.Examples: Input : arr[] = {1, 3, 5, 10, 15, 7} Output : 45 Explanation : Product of non-primes = 150 Product of primes = 105 Input : arr[] = {3, 4, 6, 7} Output : 3 Naive
15 min read
Check if a prime number can be expressed as sum of two Prime Numbers
Given a prime number N. The task is to check if it is possible to express N as the sum of two separate prime numbers.Note: The range of N is less than 108. Examples: Input: N = 13Output: YesExplanation: The number 13 can be written as 11 + 2, here 11 and 2 are both prime.Input: N = 11Output: NoRecommended: Please try your approach on {IDE} first, b
13 min read
Check whether the sum of prime elements of the array is prime or not
Given an array having N elements. The task is to check if the sum of prime elements of the array is prime or not. Examples: Input: arr[] = {1, 2, 3} Output: Yes As there are two primes in the array i.e. 2 and 3. So, the sum of prime is 2 + 3 = 5 and 5 is also prime. Input: arr[] = {2, 3, 2, 2} Output: No Approach: First find prime number up to 10^5
10 min read
Print prime numbers with prime sum of digits in an array
Given an array arr[] and the task is to print the additive primes in an array. Additive primes: Primes such that the sum of their digits is also a prime, such as 2, 3, 7, 11, 23 are additive primes but not 13, 19, 31 etc.Examples: Input: arr[] = {2, 4, 6, 11, 12, 18, 7} Output: 2, 11, 7 Input: arr[] = {2, 3, 19, 13, 25, 7} Output: 2, 3, 7 A simple
8 min read
Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
Given an integer N, the task is to print N integers ? 109 such that no two consecutive of these integers are co-prime and every 3 consecutive are co-prime. Examples: Input: N = 3 Output: 6 15 10Input: N = 4 Output: 6 15 35 14 Approach: We can just multiply consecutive primes and for the last number just multiply the gcd(last, last-1) * 2. We do thi
15+ min read
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
Given a number X and an array of N numbers. The task is to print all the numbers in the array whose set of prime factors is a subset of the set of the prime factors of X. Examples: Input: X = 60, a[] = {2, 5, 10, 7, 17} Output: 2 5 10 Set of prime factors of 60: {2, 3, 5} Set of prime factors of 2: {2} Set of prime factors of 5: {5} Set of prime fa
13 min read
Article Tags :
Practice Tags :
Trending in News
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
'); $('.spinner-loading-overlay').show(); jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id, check: true }), success:function(result) { jQuery.ajax({ url: writeApiUrl + 'suggestions/auth/' + `${post_id}/`, type: "GET", dataType: 'json', xhrFields: { withCredentials: true }, success: function (result) { $('.spinner-loading-overlay:eq(0)').remove(); var commentArray = result; if(commentArray === null || commentArray.length === 0) { // when no reason is availaible then user will redirected directly make the improvment. // call to api create-improvement-post $('body').append('
'); $('.spinner-loading-overlay').show(); jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id, }), success:function(result) { $('.spinner-loading-overlay:eq(0)').remove(); $('.improve-modal--overlay').hide(); $('.unlocked-status--improve-modal-content').css("display","none"); $('.create-improvement-redirection-to-write').attr('href',writeUrl + 'improve-post/' + `${result.id}` + '/', '_blank'); $('.create-improvement-redirection-to-write')[0].click(); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); return; } var improvement_reason_html = ""; for(var comment of commentArray) { // loop creating improvement reason list markup var comment_id = comment['id']; var comment_text = comment['suggestion']; improvement_reason_html += `
${comment_text}
`; } $('.improvement-reasons_wrapper').html(improvement_reason_html); $('.improvement-bottom-btn').html("Create Improvement"); $('.improve-modal--improvement').hide(); $('.improvement-reason-modal').show(); }, error: function(e){ $('.spinner-loading-overlay:eq(0)').remove(); // stop loader when ajax failed; }, }); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); } else { if(loginData && !loginData.isLoggedIn) { $('.improve-modal--overlay').hide(); if ($('.header-main__wrapper').find('.header-main__signup.login-modal-btn').length) { $('.header-main__wrapper').find('.header-main__signup.login-modal-btn').click(); } return; } } }); $('.left-arrow-icon_wrapper').on('click',function(){ if($('.improve-modal--suggestion').is(":visible")) $('.improve-modal--suggestion').hide(); else{ $('.improvement-reason-modal').hide(); } $('.improve-modal--improvement').show(); }); function loadScript(src, callback) { var script = document.createElement('script'); script.src = src; script.onload = callback; document.head.appendChild(script); } function suggestionCall() { var suggest_val = $.trim($("#suggestion-section-textarea").val()); var array_String= suggest_val.split(" ") var gCaptchaToken = $("#g-recaptcha-response-suggestion-form").val(); var error_msg = false; if(suggest_val != "" && array_String.length >=4){ if(suggest_val.length <= 2000){ var payload = { "gfg_post_id" : `${post_id}`, "suggestion" : `
${suggest_val}
`, } if(!loginData || !loginData.isLoggedIn) // User is not logged in payload["g-recaptcha-token"] = gCaptchaToken jQuery.ajax({ type:'post', url: "https://apiwrite.geeksforgeeks.org/suggestions/auth/create/", xhrFields: { withCredentials: true }, crossDomain: true, contentType:'application/json', data: JSON.stringify(payload), success:function(data) { jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-section-textarea').val(""); jQuery('.suggest-bottom-btn').css("display","none"); // Update the modal content const modalSection = document.querySelector('.suggestion-modal-section'); modalSection.innerHTML = `
Thank You!
Your suggestions are valuable to us.
You can now also contribute to the GeeksforGeeks community by creating improvement and help your fellow geeks.
`; }, error:function(data) { jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-modal-alert').html("Something went wrong."); jQuery('#suggestion-modal-alert').show(); error_msg = true; } }); } else{ jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-modal-alert').html("Minimum 5 Words and Maximum Character limit is 2000."); jQuery('#suggestion-modal-alert').show(); jQuery('#suggestion-section-textarea').focus(); error_msg = true; } } else{ jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-modal-alert').html("Enter atleast four words !"); jQuery('#suggestion-modal-alert').show(); jQuery('#suggestion-section-textarea').focus(); error_msg = true; } if(error_msg){ setTimeout(() => { jQuery('#suggestion-section-textarea').focus(); jQuery('#suggestion-modal-alert').hide(); }, 3000); } } document.querySelector('.suggest-bottom-btn').addEventListener('click', function(){ jQuery('body').append('
'); jQuery('.spinner-loading-overlay').show(); if(loginData && loginData.isLoggedIn) { suggestionCall(); return; } // load the captcha script and set the token loadScript('https://www.google.com/recaptcha/api.js?render=6LdMFNUZAAAAAIuRtzg0piOT-qXCbDF-iQiUi9KY',[], function() { setGoogleRecaptcha(); }); }); $('.improvement-bottom-btn.create-improvement-btn').click(function() { //create improvement button is clicked $('body').append('
'); $('.spinner-loading-overlay').show(); // send this option via create-improvement-post api jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id }), success:function(result) { $('.spinner-loading-overlay:eq(0)').remove(); $('.improve-modal--overlay').hide(); $('.improvement-reason-modal').hide(); $('.create-improvement-redirection-to-write').attr('href',writeUrl + 'improve-post/' + `${result.id}` + '/', '_blank'); $('.create-improvement-redirection-to-write')[0].click(); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); });
Continue without supporting 😢