Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:02:47+00:00 2026-06-11T11:02:47+00:00

I need to implement a recursive function that returns 1 if the number is

  • 0

I need to implement a recursive function that returns 1 if the number is prime or 0 otherwise. The homework problem says that I can’t use ‘%’ mod. Haskell should be something like this… I’m not sure.

isprime x = prime(x sqrt(x))

prime x i = | i==1 = 1
            | mod(x i)==0 = 0
            | otherwise = prime(x i-1)

mod num div | num<div = n
            | otherwise = mod(num-div div)

I tested an algorithm in C because I don’t have a Haskell compiler on my Mac, but there’s something wrong because it’s returning false positive on primes-1. idk why.

int main (int argc, const char * argv[]){
    int a=0,b=31;
    printf("\n Prime numbers between %d and %d \n",a,b);

    for(int a=0; a<=b; a++){
        if(isPrime(a)==0){
            printf("%d, ",a);
        } 
    }
    return 0;
}

int isPrime(int x){
    return prime(x, sqrt(x));
}

int prime(int x, int i){
    if(i==0){
        return 0;
    }
    else if(mod(x,i)==1){
        return 1;
    }
    else{
        return prime(x, i-1);
    }
}

int mod(int num, int div){
    if(num<div) return num;
    else return mod(num-div, div);
}

The algorithm is returning this:

Prime numbers between 0 and 31 
0, 1, 2, 3, 4, 6, 8, 12, 14, 18, 20, 24, 30,
Program ended with exit code: 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:02:48+00:00Added an answer on June 11, 2026 at 11:02 am

    (apparently, there’s a new policy regarding homework, viz. “If you don’t want a fully vetted, complete and testable answer, Stack Overflow is not the place to ask – by Tim Post”, so here goes).

    Basically, your code is almost correct (1 is not a prime), sans some syntax issues.

    isprime x = prime x (floor $ sqrt $ fromIntegral x)   where
      prime x i | i==1 && x > 1  = 1
                | x == i*div x i = 0
                | otherwise      = prime x (i-1)
    
    -- mod x i = x - i*div x i
    -- mod x i == 0 = x == i*div x i
    

    fromIntegral is just some adaptor which lets us use an Integral value as an argument to sqrt which expects a Floating argument. Try using :i sqrt or :i Integral etc. at the GHCi prompt (also read some documentation google around).

    But algorithmically there’s place for improvement. First of all, it’s much better to try out the divisors in the other direction, from 2 up to the number’s sqrt, because any given number is more likely to have a smaller factor than a larger one. Second, after trying out 2, there’s no need to try out any other even number as a possible divisor. This gives us

    isprime x | x == 2          = 1
              | x < 2 || even x = 0
              | otherwise       = go 3
      where
        r = floor $ sqrt $ fromIntegral x
        go i | i > r          = 1
             | x == i*div x i = 0        -- really, | rem x i == 0 = 0
             | otherwise      = go (i+2)
    

    This would normally be written down using Bools, and a higher-order function like and which captures the recursions and testing pattern (so it is not recursive anymore):

    isprime x = if isPrime x then 1 else 0
    
    isPrime x = x==2 || x>2 && odd x && 
                  and [rem x d /= 0 | d <- [3,5..floor $ sqrt $ fromIntegral x]]
    

    There’s some redundancy in there still: after we’ve tested by 3, there’s no need to test by any of its multiples too (just like we did with 2 and evens). We really just need to test by prime factors:

    isPrime x = x>1 && and 
        [rem x d /= 0 | d <- takeWhile (<= (floor $ sqrt $ fromIntegral x)) primes]
    
    primes = filter isPrime [2..]
           = 2 : filter isPrime ([3..] `minus` [4,6..])
           = 2 : filter isPrime [3,5..]
           = 2 : 3 : filter isPrime ([5,7..] `minus` [9,15..])
           = 2 : 3 : 5 : filter isPrime (([7,9..]`minus`[9,15..])`minus`[25,35..])
           ...........
    

    Here we see the emergence of the sieve of Eratosthenes, P = {3,5, …} \ U {{p2, p2 + 2p, …} | p in P} (w/out the 2).

    see also:

    • http://en.wikipedia.org/wiki/Haskell_features#Prime_numbers
    • http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
    • http://www.haskell.org/haskellwiki/Prime_numbers
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a recursive function, that would print a class signature, including class name
I have a recursive function (in C#) that I need to call about 800
As topic said, I need a recursive function that split a string if its
I need to implement a thread pool in Java (java.util.concurrent) whose number of threads
i need to implement a messaging scenario that consumes messages from throusands of destinations
I need to implement quicksort in SML for a homework assignment, and I'm lost.
I'm writing a function in C that takes a variable number of arguments. size_t
I have following challenge: Implement a function that searches for a null point of
I'm trying to implement a function that initializes an arbitrarily sized list with objects,
I need to implement portable code, but I do not know how to deal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.