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 8656851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:22:49+00:00 2026-06-12T15:22:49+00:00

I’m learning C by translating some of the things I have done in Python

  • 0

I’m learning C by translating some of the things I have done in Python to C. I have tried to look online as much as I can before coming here but it seems really difficult to find answers to what I’m looking for.

What follows is my (so-far) translation of the Miller-Rabin test for a number’s primality.

#include <stdio.h>
#include <math.h>

_Bool prime(long long n);

int main() {
    int i = 6;
    for (i; i < 9; i++) {
        if (prime(i)) {
            printf("%i\n", prime(i));
        }
    }
}

_Bool prime(long long n) {
    int s = 0;
    int r = 0;
    int a_index = 0;
    // printf("the value of a_index when initialised is: %d\n", a_index);
    // printf("the value of s when initialised is: %d\n", s);
    int *a_list;
    _Bool is_prime = 1;
    _Bool composite_part_a = 1;
    _Bool composite_part_b = 1;
    long long d = n - 1;
    while (d % 2 == 0) {
        s++;
        d = d / 2;
    }

    if (4759123141 <= n && n < 2152302898747) {
        // malloc
        a_list[0] = 2;
        a_list[1] = 3;
        a_list[2] = 5;
        a_list[3] = 7;
        a_list[4] = 11;
    }
    else if (9080191 <= n && n < 4759123141) {
        // malloc
        a_list[0] = 2;
        a_list[1] = 7;
        a_list[2] = 61;
    }
    else if (1373653 <= n && n < 9080191) {
        // malloc
        a_list[0] = 31;
        a_list[1] = 73;
    }           
    else if (4 <= n && n < 1373653) {
        a_list = (int *) malloc(sizeof(int) * 2);
        a_list[0] = 2;
        a_list[1] = 3;
        printf("the value of a_list[0] upon its first assignment is: %d\n", a_list[0]);
        // printf("the first element of a_list is: %d\n", a_list[0]);
        // printf("the second element of a_list is: %d\n", a_list[1]);
    }
    else if (n == 3 | n == 2) {
        return 1;
    }
    else if (n % 2 == 0 | n == 1) {
        return 0;
    }

    printf("the value of a_list[0] over here is: %d\n", a_list[0]);
    // printf("%d\n", a_list[1]);  
    for (a_index; a_index < sizeof(a_list) / sizeof(int); a_index++) {
        printf("test");
        if ((long long)pow(a_index[a_list], d) % n != 1) {
            composite_part_a = 1;
        }
        else {
            composite_part_a = 0;
        }


        // printf("the value of r is: %d\n", r);
        // printf("the value of s is: %d\n", s);
        for (r; r < s; r++) {
            printf("%lld\n", (int)pow(a_list[a_index], exp2(r) * d) % n);
            if ((long long)pow(a_index[a_list], exp2(r) * d) % n != -1) {
                composite_part_b = 1;
            }
            else {
                composite_part_b = 0;
                break;
            }
            }

        if (composite_part_a && composite_part_b) {
            return 0;
        }
        }

    return is_prime;
    }

The trouble with learning C is there isn’t much good literature for pure beginners, outside of what I hear about K&R but that’s in the mail and I can’t get my hands on it right now. The program returns these errors:

3.c: In function ‘prime’:
3.c:52:26: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
/tmp/ccGQnk9T.o: In function `prime':
3.c:(.text+0x272): undefined reference to `pow'
3.c:(.text+0x2b9): undefined reference to `exp2'
3.c:(.text+0x2db): undefined reference to `pow'
3.c:(.text+0x30b): undefined reference to `exp2'
3.c:(.text+0x32d): undefined reference to `pow'
collect2: ld returned 1 exit status

First off, haven’t I included to introduce pow and else? I know it’s not proper to ask two questions and my main question is about pow and exp2, but if you do have a suggestion about the malloc as well feel free to include it.

  • 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-12T15:22:50+00:00Added an answer on June 12, 2026 at 3:22 pm

    You need to link with the math library as well, it’s not included by default.

    Something like the following command:

    $ gcc 3.c -lm
    

    Notice the -lm argument… It tells the linker to add a library (the -l part) and the name of the library (the m part).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.