I want to print prime number - python-3.6

I want to print prime number, but in my code print() showing invalid syntax.My question is about, I can not understand why it is showing. So please help me what should be modify needed in the code and please tell me code is wright or wrong.
def isPrime(num):
for i in range(2,num):
if num % i == 0 and num % 1 == 0:
return True
else:
return False
else:
return None
num = int(input("Enter a number:")
print(isPrime(num))

num = int(input("Enter a number:")
You missed a parentheses. It should be:
num = int(input("Enter a number:"))

You are missing a parenthesis here:
num = int(input("Enter a number:")
it should be:
num = int(input("Enter a number:"))

Related

I want to ask the user to input an integer and check it and return an error message if it isn't in R

now I thought I would simply use is.integer and use the TRUE or FALSE output as a condition for my error message, but it seems even integers give FALSE cause R stores them float or something (e.g. 5) but if I change the input to as.integer then all inputs become integers thus defeating the purpose of the program.
num = readline(prompt = "Enter a number: ")
y = num
x <- is.integer(num)
if (y > 0 && x == FALSE) {
print("Please enter a natural number")
}
readline stores user input as a character. You can use as.numeric() for your case. Input will become NA if it cannot be converted to numeric. Integrality can be tested using round(num) != num.
num <- as.numeric(readline(prompt = "Enter a number: "))
if (is.na(num) | num <= 0 | round(num) != num) {
print("Please enter a natural number")
}

How to optimize this recursion function?

This is really similar to Fibonacci Sequence problem. I understand the DP optimization with Fibonacci function using the for loop, but I'm having hard time to connect to this problem.
The recursion function I want to optimize is:
def cal(n):
if n <= 0:
return 1
else:
return cal(n-25)+cal(n-26)
Something like this may help:
(It's inspired by previous post)
from functools import cache
#cache
def cal(n):
if n <= 0:
return 1
else:
return cal(n-25) + cal(n-26)
print(cal(100))
The idea of a "for loop optimization" is that we calculate cal(0), cal(1), cal(2), ... consecutively. And when we want cal(n), we already have cal(n-25) and cal(n-26) stored in an array.
So, the following solution has linear complexity for non-negative n:
def cal(n):
mem = [1] # cal(0) is 1
for i in range(1, n + 1):
num = 1 if i - 25 < 0 else mem[i - 25]
num += 1 if i - 26 < 0 else mem[i - 26]
mem.append (num)
return mem[-1]
One can further optimize to make all the values cal(1), cal(2), ..., cal(n) globally available after calculating the last of them.

function is getting executed for a invalid argument

Below is the function created to format number.
format_number <- function(num, format_flag = "yes"){
if(format_flag == "yes"){
num <- paste0(num/100,"%")
} else if (format_flag == "no"){
num <- num
}
return(num)
}
However, when I execute format_number(3,2), the answer is 3. Basically it should show an error right? Because 2 is not a valid input. Please advice
2 is valid input. but in your function none of "if" statement will satisfied because 2!='yes' and 2!='no' , then the original value of 'num' will return which is 3.

Subset sum using recursive backtracking in python using list and return statements.

I am trying to solve the subset sum problem using recursive back-tracking. While it works if I print the result instantly, it does not seem to work if I try to return it.
Here is a look at the code in python. I have added intermediate print statements to make things easier(please uncomment them):
l = [int(i) for i in input().split()]
num = int(input())
#print(num)
#print(l)
def subset(l):
tot = 0
ch = []
return(subsetHelper(l,tot,num,ch))
def subsetHelper(l,tot,num,ch):
#print("("+str(l)+" "+str(tot)+" "+str(num)+" "+str(ch)+")")
if(tot==num):
return(ch)
elif(tot>num or len(l)==0):
return
else:
ch.append(l[0])
return subsetHelper(l[1:],tot+l[0],num,ch)
del ch[-1]
return subsetHelper(l[1:],tot,num,ch)
print(subset(l))
The same concept works when I print the result without returning it.
l = [int(i) for i in input().split()]
num = int(input())
#print(num)
#print(l)
def subset(l):
tot = 0
ch = []
subsetHelper(l,tot,num,ch)
def subsetHelper(l,tot,num,ch):
#print("("+str(l)+" "+str(tot)+" "+str(num)+" "+str(ch)+")")
if(tot==num):
print(ch)
elif(tot>num or len(l)==0):
#return "Hello"
return
else:
ch.append(l[0])
subsetHelper(l[1:],tot+l[0],num,ch)
del ch[-1]
subsetHelper(l[1:],tot,num,ch)
subset(l)
Any suggestions?

Math: removing a digit from a number

Let's say that I have the following number: 1286 now I want to remove the last digit 6 and end up with the first 3 digits 128 only. I also would like to do this using the number 6 and the number 1286 only as inputs.
Also if there is a c# solution would be great.
Thanks
Edit:
I want to do this using a math equation between the 6 and the 1286, I already know how to parse strings, which is not what I'm after.
please try the below code: (this is done only using mathematical functions, also I don't know C#)
java.util.Scanner s=new java.util.Scanner(System.in);
int last=s.nextInt();
int firstNumber=s.nextInt();
int ans=0;
loop:
for(int temp=firstNumber,i=0;temp>0;temp/=10)
{
if(temp%10==last){ans=temp/10;while(i>0){ans=ans*10+(i%10);i/=10;} break loop;}
i=i*10;
i=i+(temp%10);
}
if(ans>0)System.out.println(ans);
}
}
string input = "OneTwoThree";
// Get first three characters
string sub = input.Substring(0, 3);
sub will now have the first 3 chars from the string, the 0 is the start pos, and then how many chars do you want (ie: 3) - this is where the (0, 3) come in to it - if you had (3, 3), sub would equal "Two"
I think this might be what you are looking for :)
In JavaScript: Here is your number:
var num = 1286;
This line removes the last digit:
num % 10; //returns 6
And these two lines remove the 6:
num /= 10 // turns num to 128.6
num = Math.trunc(num) // num now equals 128
Better yet, you could put it in a function, like so:
function sumOfDigits(num) {
const sumArr = [];
while (num > 0) {
sumArr.push(num % 10);
num /= 10;
num = Math.trunc(num);
}
return sumArr.reduce((a, b) => a + b, 0);
}
sumOfDigits(1234);
// returns 10
Hope this helps.

Resources