Untitled

mail@pastecode.io avatar
unknown
plain_text
3 months ago
3.0 kB
5
Indexable
Never
---HTML--

<h2>Granville Numbers</h2>
<br />
Enter a Positive Number: <input type="number" id="pos_number"/>
<button id="subButton">Submit</button>
<p id="ans_num">The Number is: </p>

---JS---
$().ready(function () {
    $('#subButton').click(function () {
        var pos_number = $('#pos_number').val();
        if (!pos_number || isNaN(pos_number)) {
            alert("Please enter a valid positive number.");
            return;
        }
        $.post('../Home/postAjaxSample',
            {
                pos_number: pos_number,
            },
            function (data) {
                if (data.error) {
                    alert(data.error);
                } else {
                    var result = data[0].isGranville ? 'Granville' : 'Not Granville';
                    $('#ans_num').text('The Number is: ' + result);
                }
            }
        );
    });
});


----CS--
        public ActionResult AjaxSample()
        {
            return View();
        }

        public ActionResult postAjaxSample()
        {
            var data = new List<object>();
            var num = Request["pos_number"];

            if (string.IsNullOrEmpty(num))
            {
                return Json(new { error = "Missing number parameter" }, JsonRequestBehavior.AllowGet);
            }

            int number;
            if (!int.TryParse(num, out number))
            {
                return Json(new { error = "Invalid number format" }, JsonRequestBehavior.AllowGet);
            }

            if (number > 0)
            {
                bool isGranville = IsGranvilleNumber(number);

                data.Add(new
                {
                    number = number,
                    isGranville = isGranville
                });
            }
            else
            {
                return Json(new { error = "Number must be positive" }, JsonRequestBehavior.AllowGet);
            }

            return Json(data, JsonRequestBehavior.AllowGet);
        }

        private bool IsGranvilleNumber(int num)
        {
            if (num <= 1)
            {
                return false;
            }

            int divisorCount = 2; 
            List<int> primeFactors = new List<int>();

            for (int i = 3; i <= num / 2; i += 2) 
            {
                if (num % i == 0)
                {
                    divisorCount++;
                    if (IsPrime(i))
                    {
                        primeFactors.Add(i);
                    }
                }
            }
            return divisorCount == num && primeFactors.Distinct().Count() == 3;
        }

        private bool IsPrime(int num)
        {
            if (num <= 1)
            {
                return false;
            }
            for (int i = 2; i <= Math.Sqrt(num); i++)
            {
                if (num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
Leave a Comment